Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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对象_Javascript_Json_Ecmascript 6 - Fatal编程技术网

无法减少数组中的相关JavaScript对象

无法减少数组中的相关JavaScript对象,javascript,json,ecmascript-6,Javascript,Json,Ecmascript 6,我试图按键将数组中的相关JavaScript对象分组 但是,我在一个点上卡住了 我有一个JavaScript对象数组,如下所示: [ { "text": "hello world", "startPos": 0, "endPos": 12, "value": "hello world", "entity": "Greeting" }, { "text": "hello worl

我试图按键将数组中的相关JavaScript对象分组

但是,我在一个点上卡住了

我有一个JavaScript对象数组,如下所示:

[
    {
        "text": "hello world",
        "startPos": 0,
        "endPos": 12,
        "value": "hello world",
        "entity": "Greeting"
    },
    {
        "text": "hello world",
        "startPos": 0,
        "endPos": 6,
        "value": "hello",
        "entity": "Greeting"
    }
]
我编写了以下代码,但我被卡住了

a=[
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:12,
“值”:“你好,世界”,
“实体”:“问候语”
},
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:6,
“值”:“你好”,
“实体”:“问候语”
}
]
让结果=a.减少((acc,d)=>{
const found=acc.find(a=>a.text==d.text);
常量实体数组={
startPos:d.startPos,
endPos:d.endPos,
实体:d.实体
};
如果(找到){
已找到.entities.push(entitiesArray);
}否则{
acc.push({});
}
返回acc;
}, []);

log(JSON.stringify(result,未定义,4))您可以添加一个带有文本的新对象和一个实体数组

此解决方案获取一个已破坏的对象,并使用键构建新对象

var a=[{text:“hello world”,startPos:0,endPos:12,value:“hello world”,entity:“Greeting”},{text:“hello world”,startPos:0,endPos:6,value:“hello”,entity:“Greeting”},
结果=a.reduce((acc,{text,startPos,endPos,entity,value})=>{
var found=acc.find(a=>a.text==text),
子实体={startPos,endPos,entity,value};
如果(找到){
找到.实体.推送(子实体);
}否则{
acc.push({文本,实体:[子实体]});
}
返回acc;
}, []);
控制台日志(结果)

.as console wrapper{max height:100%!important;top:0;}
您可以添加一个新对象,其中包含文本和实体数组

此解决方案获取一个已破坏的对象,并使用键构建新对象

var a=[{text:“hello world”,startPos:0,endPos:12,value:“hello world”,entity:“Greeting”},{text:“hello world”,startPos:0,endPos:6,value:“hello”,entity:“Greeting”},
结果=a.reduce((acc,{text,startPos,endPos,entity,value})=>{
var found=acc.find(a=>a.text==text),
子实体={startPos,endPos,entity,value};
如果(找到){
找到.实体.推送(子实体);
}否则{
acc.push({文本,实体:[子实体]});
}
返回acc;
}, []);
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
found
为false时,您没有将带有
文本的对象添加到数组中。所以在下一次迭代中,它无法找到比较文本的对象

此外,如果您的IDE和引擎支持新的ES方案,则可以使用

const a=[
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:12,
“值”:“你好,世界”,
“实体”:“问候语”
},
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:6,
“值”:“你好”,
“实体”:“问候语”
}
]
让结果=a.减少((acc,d)=>{
const found=acc.find(a=>a.text==d.text);

const{text,…entitiesArray}={…d};//当
found
为false时,您没有将
text
的对象添加到数组中。因此在下一次迭代中,它无法通过比较它们的文本找到对象

此外,如果您的IDE和引擎支持新的ES方案,则可以使用

const a=[
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:12,
“值”:“你好,世界”,
“实体”:“问候语”
},
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:6,
“值”:“你好”,
“实体”:“问候语”
}
]
让结果=a.减少((acc,d)=>{
const found=acc.find(a=>a.text==d.text);

const{text,…entitiesArray}={…d};//您还可以使用
reduce
Object.values

a=[{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:12,
“值”:“你好,世界”,
“实体”:“问候语”
},
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:6,
“值”:“你好”,
“实体”:“问候语”
}
];
让结果=对象。值(a.reduce)(c,v)=>{
c[v.text]=c[v.text]|{text:v.text,实体:[]};
c[v.text].entities.push(v);
返回c;
}, {}));

console.log(result);
您还可以使用
reduce
Object.values

a=[{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:12,
“值”:“你好,世界”,
“实体”:“问候语”
},
{
“文本”:“你好,世界”,
“startPos”:0,
“endPos”:6,
“值”:“你好”,
“实体”:“问候语”
}
];
让结果=对象。值(a.reduce)(c,v)=>{
c[v.text]=c[v.text]|{text:v.text,实体:[]};
c[v.text].entities.push(v);
返回c;
}, {}));

console.log(result);
您希望第二个对象中出现
“text”:“hello world”
?还是复制粘贴问题为什么第一个实体没有
text
而第二个实体没有
实体
?不,我得到两个“不同的”其
text
键相同的对象,我想对它们进行分组。但是您在实体上拥有的对象(所需输出)不同于您所需的对象started@Eddie更新了问题。您是否希望
“文本”:“hello world”
在第二个对象中?或者是复制粘贴问题为什么第一个实体没有
文本
,而第二个实体没有
实体
?不,我得到了两个
文本
键相同的“不同”对象,我想对它们进行分组。但是实体上的对象(所需输出)和你的不同started@Eddie更新了问题。
const entitiesArray=Object.assign({},d);
它复制了整个对象,这个对象也有
text
键,我只想要所选的属性,如问题中所述。您的输入d