Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Node.js中使用多个对象来归档JSON数据?_Javascript_Node.js - Fatal编程技术网

Javascript 如何在Node.js中使用多个对象来归档JSON数据?

Javascript 如何在Node.js中使用多个对象来归档JSON数据?,javascript,node.js,Javascript,Node.js,我有一个JSON数据数组 [ { _id: 5f2084f656dc2428e96fbe92, category: [ '5e5150c6c52a3904b74d6ff7', null ], coordinates: { lat: 37.8025909, lng: -122.4336412 }, location: { type: 'Point', coordinates: [Array] }, calcDistance: 3323.833296768

我有一个JSON数据数组

[
  {
    _id: 5f2084f656dc2428e96fbe92,
    category: [ '5e5150c6c52a3904b74d6ff7', null ],
    coordinates: { lat: 37.8025909, lng: -122.4336412 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3323.833296768243
  },
  {
    _id: 5f2084fb56dc2428e9703f3b,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7683708, lng: -122.4564649 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3340.838606150309
  },
  {
    _id: 5f2084f656dc2428e96fbe86,
    category: [ '5e5150c6c52a3904b74d6ff7', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fb56dc2428e9703f3f,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fa56dc2428e9702130,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7669182, lng: -122.4561008 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3349.000719541262
  },
  {
    _id: 5f2084fb56dc2428e9703ee5,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7787325, lng: -122.4572683 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3357.3418413767595
  },
  {
    _id: 5f2084f856dc2428e96ff091,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3377.281713590449
  },
  {
    _id: 5f2084fb56dc2428e9703f73,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8049528, lng: -122.4249378 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3377.281713590449
  },
  {
    _id: 5f2084fb56dc2428e9703f39,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.7788351, lng: -122.4575644 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3384.6341862170793
  },
  {
    _id: 5f14aa92745456119cf27701,
    category: [ '5e5150f0c52a3904b74d6ffb', null ],
    coordinates: { lat: 37.779187, lng: -122.4577491 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3405.9730381738473
  },
  {
    _id: 5f2084fb56dc2428e9703f21,
    category: [ '5e5150bcc52a3904b74d6ff6', null ],
    coordinates: { lat: 37.8034776, lng: -122.4027084 },
    location: { type: 'Point', coordinates: [Array] },
    calcDistance: 3501.3565554464535
  },
]
我想找到一个对象,该对象的类别为返回的“favTypes”

"5e5150bcc52a3904b74d6ff6,5e5150c6c52a3904b74d6ff7,5e5150d6c52a3904b74d6ff8"
正在搜索其类别中具有这三个对象Id之一的对象。我试过了

jsondatas.filter(jsondata=>word.category=favTypes);
但是,它返回的是jsondatas not筛选对象。是否有任何方法可以对每次迭代作为数组或整个字符串indexOf(),.includes()或.match()进行过滤?

您必须使用“==”而不是“=”


“jsondatas.filter(jsondata=>word.category==favTypes)”

您可以使用嵌套的foreach并添加一些条件,然后将其推送到初始数组。请参见下面的代码

const arr=[
{
_id:“5f2084f656dc2428e96fbe92”,
类别:[“5e5150c6c52a3904b74d6ff7”,空],
座标:{lat:37.8025909,lng:-122.4336412},
位置:{类型:“点”,坐标:[数组]},
距离:3323.833296768243,
},
{
_id:“5f2084fb56dc2428e9703f3b”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.7683708,lng:-122.4564649},
位置:{类型:“点”,坐标:[数组]},
距离:3340.838606150309,
},
{
_id:“5f2084f656dc2428e96fbe86”,
类别:[“5e5150c6c52a3904b74d6ff7”,空],
坐标:{lat:37.7669182,lng:-122.4561008},
位置:{类型:“点”,坐标:[数组]},
距离:3349.000719541262,
},
{
_id:“5f2084fb56dc2428e9703f3f”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.7669182,lng:-122.4561008},
位置:{类型:“点”,坐标:[数组]},
距离:3349.000719541262,
},
{
_id:“5f2084fa56dc2428e9702130”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.7669182,lng:-122.4561008},
位置:{类型:“点”,坐标:[数组]},
距离:3349.000719541262,
},
{
_id:“5f2084fb56dc2428e9703ee5”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.7787325,lng:-122.4572683},
位置:{类型:“点”,坐标:[数组]},
距离:3357.3418413767595,
},
{
_id:“5f2084f856dc2428e96ff091”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.8049528,lng:-122.4249378},
位置:{类型:“点”,坐标:[数组]},
距离:3377.281713590449,
},
{
_id:“5f2084fb56dc2428e9703f73”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.8049528,lng:-122.4249378},
位置:{类型:“点”,坐标:[数组]},
距离:3377.281713590449,
},
{
_id:“5f2084fb56dc2428e9703f39”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
坐标:{lat:37.7788351,lng:-122.4575644},
位置:{类型:“点”,坐标:[数组]},
距离:3384.6341862170793,
},
{
_id:“5f14aa92745456119cf27701”,
类别:[“5e5150f0c52a3904b74d6ffb”,空],
座标:{lat:37.779187,lng:-122.4577491},
位置:{类型:“点”,坐标:[数组]},
距离:3405.9730381738473,
},
{
_id:“5f2084fb56dc2428e9703f21”,
类别:[“5e5150bcc52a3904b74d6ff6”,空],
座标:{lat:37.8034776,lng:-122.4027084},
位置:{类型:“点”,坐标:[数组]},
距离:3501.3565554464535,
},
];
常量数组过滤器=[
“5e5150bcc52a3904b74d6ff6”,
“5e5150c6c52a3904b74d6ff7”,
“5e5150d6c52a3904b74d6ff8”,
];
//初始化新数组
常数newArr=[];
arr.forEach((x)=>{
arrayToFilter.forEach((y)=>{
如果(x.category[0]==y){
//将项目推送到已初始化的位置
//如果项目的键匹配
新推力(x);
}
});
});
//你也可以用这个
//arr.forEach((x)=>
//arrayToFilter.forEach((y)=>x.category[0]==y&&newArr.push(x))
//);
控制台日志(newArr)过滤器将返回满足匹配条件的对象(json)。你到底需要什么?