Node.js 为什么从定制模块导入的函数在JSFIDLE中工作时表现不同?

Node.js 为什么从定制模块导入的函数在JSFIDLE中工作时表现不同?,node.js,lodash,Node.js,Lodash,我有两个文件index.js和sort.js js文件包含一个变量“x”,其值是英语单词对象的数组。每个对象都包含一个“pos”属性,该属性也是一个数组,其值可以是n、v、adj、adv(至少包含一个值) sort.js包含一个以变量“x”为参数的函数。然后,它以这样的方式排列单词:每个pos值在主数组中成为一个单独的数组 代码正在工作,但在我的项目中得到一个空数组作为输出。我在sort.js文件中尝试了不同的导出方法,如module.exports等 注意:我使用lodash作为依赖项 以下是

我有两个文件index.js和sort.js

js文件包含一个变量“x”,其值是英语单词对象的数组。每个对象都包含一个“pos”属性,该属性也是一个数组,其值可以是n、v、adj、adv(至少包含一个值)

sort.js包含一个以变量“x”为参数的函数。然后,它以这样的方式排列单词:每个pos值在主数组中成为一个单独的数组

代码正在工作,但在我的项目中得到一个空数组作为输出。我在sort.js文件中尝试了不同的导出方法,如module.exports等

注意:我使用lodash作为依赖项

以下是两个文件的代码:

index.js

const _ = require("lodash");
const { sortByPos } = require("../lib/sort");

/* 
 Some extra code here
*/

let x = [
    {
      word: "music",
      pos: ["n"],
      first: "m",
      wordCount: 1,
      len: 5
    },
    {
      word: "scale",
      pos: ["n", "v"],
      first: "s",
      wordCount: 1,
      len: 5
    },
    {
      word: "beats",
      pos: ["n"],
      first: "b",
      wordCount: 1,
      len: 5
    },
    {
      word: "surmount",
      pos: ["v"],
      first: "s",
      wordCount: 1,
      len: 8
    },
    {
      word: "euphony",
      pos: ["n", "adv"],
      first: "e",
      wordCount: 1,
      len: 7
    },
    {
      word: "trounce",
      pos: ["adj", "v"],
      first: "t",
      wordCount: 1,
      len: 7
    }
  ];

  console.log(sortByPos(x))
sort.js

const _ = require("lodash");
exports.sortByPos = words => {
 return _.reduce(
 words,
 (result, obj) => {
   _.forEach(obj.pos, el => (result[el] || (result[el] = [])).push(obj));
   return result;
 },
 []
 );
};

更新:代码在JSFIDLE中也不能正常工作。

您的reduce累加器(结果)是一个数组,但您为它分配了非数字键。相反,使用对象作为累加器,如果需要数组,请使用
.values()
object.values()
将其转换为数组:

const sortByPos=words=>{
返回u0.reduce(
话,
(结果,obj)=>{
_.forEach(obj.pos,el=>(结果[el]| |(结果[el]=])).push(obj));
返回结果;
}, {}
);
};
设x=[{word:“music”,“pos:[“n”],“first:“m”,“wordCount:”1,“len:”5},{“word:”scale”,“pos:”n”,“v“,”first:”s”,“wordCount:”1,“len:”5},{“word:”b”,“wordCount:”1,“len:”5},{“word word:”超越”,“pos:”v“,”first:”s”,“wordCount:”s”,“wordCount:”1,“len:”8},{“word”“word”,“word:”谐音”,“advonn”,“advon”,“wordCount”,“word:”,”1:”7},{“trounce”,“pos”:[“adj”,“v”],“first”:“t”,“wordCount”:1,“len”:7}];
log(sortByPos(x))//数组的对象
console.log(u.values(sortByPos(x))//一个数组数组
您的reduce累加器(
结果
)是一个数组,但您为其分配了非数字键。请使用对象作为累加器,如果需要数组,请使用
.values()
object.values()
将其转换为数组:

const sortByPos=words=>{
返回u0.reduce(
话,
(结果,obj)=>{
_.forEach(obj.pos,el=>(结果[el]| |(结果[el]=])).push(obj));
返回结果;
}, {}
);
};
设x=[{word:“music”,“pos:[“n”],“first:“m”,“wordCount:”1,“len:”5},{“word:”scale”,“pos:”n”,“v“,”first:”s”,“wordCount:”1,“len:”5},{“word:”b”,“wordCount:”1,“len:”5},{“word word:”超越”,“pos:”v“,”first:”s”,“wordCount:”s”,“wordCount:”1,“len:”8},{“word”,“word:”谐音”,“advony”,“wordCount”,“word:”,”7:“,”第一个词:{trounce,pos:[“adj”,“v”],“first”:“t”,“wordCount”:1,“len”:7}];
log(sortByPos(x))//数组的对象
console.log(u.values(sortByPos(x))//一个数组数组

它在小提琴中不起作用。数组是对象,您可以向数组添加更多属性。但是,当您尝试将其作为数组进行迭代时,添加属性不包括在内。您可以在小提琴中看到,数组的长度为0。它在小提琴中不起作用。数组是对象,您可以向ar添加更多属性但是,当您尝试将其作为数组进行迭代时,不包括add属性。您可以在fiddle中看到,数组的长度为0。