Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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连接/Concat/Filter?_Javascript - Fatal编程技术网

JavaScript连接/Concat/Filter?

JavaScript连接/Concat/Filter?,javascript,Javascript,我正在努力寻找解决方案,我构建了这个简单的示例。我希望有人能打动我的大脑。提前谢谢 var arr = [ { name:"DOM", other: "a" }, { name:"DEE", other: "b" }, { name:"DEE", other: "c" }, { name:"DEE", other: "d" }, { na

我正在努力寻找解决方案,我构建了这个简单的示例。我希望有人能打动我的大脑。提前谢谢

var arr = [
{ name:"DOM", other: "a" },
{ name:"DEE", other: "b" },
{ name:"DEE", other: "c" },
{ name:"DEE", other: "d" },
{ name:"DEE", other: "e" },
];
预期产出:

{name: "DOM", other: "a" }
{name: "DEE", other: "b,c,d,e"}

使用
forEach
并构建一个对象,其键为
name
,值为agreegate
other
。从上面的对象获取
对象。值

var-arr=[
{name:“DOM”,other:“a”},
{名称:“DEE”,其他:“b”},
{名称:“DEE”,其他:“c”},
{名称:“DEE”,其他:“d”},
{名称:“DEE”,其他:“e”},
];
常量合并=(arr)=>{
常数res={};
arr.forEach((项目)=>{
if(res中的item.name){
res[item.name].other+=`,${item.other}`;
}否则{
res[item.name]={…item};
}
});
返回Object.values(res);
};
console.log(合并(arr))
var-arr=[
{name:“DOM”,other:“a”},
{名称:“DEE”,其他:“b”},
{名称:“DEE”,其他:“c”},
{名称:“DEE”,其他:“d”},
{名称:“DEE”,其他:“e”},
];
const result=arr.reduce((acc,curr)=>{
if(会计科目[当前名称]){
acc[curr.name].other=acc[curr.name]['other'].concat(','+curr.other)
}否则{
acc[curr.name]={name:curr.name,other:curr.other}
}
返回acc;
}, {})
console.log(结果)
.as控制台包装{max height:100%!important;top:0;}
这应该可以:

让arr=[
{name:“DOM”,other:“a”},
{名称:“DEE”,其他:“b”},
{名称:“DEE”,其他:“c”},
{名称:“DEE”,其他:“d”},
{名称:“DEE”,其他:“e”},
];
结果={}
用于(arr的let项目){
如果(结果中的item.name){
结果[item.name]+=','+item.other
}否则{
结果[item.name]=item.other
}
}
让resultArray=Object.entries(result.map)(数据)=>
{返回{名称:数据[0],其他:数据[1]})

console.log(resultArray)
到目前为止,您尝试了哪些内容,哪些内容无效?您是否因为“您”不理解而将我的问题标记为需要删除?除非您标记其他人,否则我是唯一收到您消息通知的人,并且我没有标记您的问题(您应该能够看到谁在上面的蓝色横幅中标记了它)。