Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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_Arrays_Reactjs_Ecmascript 6_Javascript Objects - Fatal编程技术网

Javascript 如何在对象中的数组中查找属性并将这些对象移动到对象中的另一个数组?

Javascript 如何在对象中的数组中查找属性并将这些对象移动到对象中的另一个数组?,javascript,arrays,reactjs,ecmascript-6,javascript-objects,Javascript,Arrays,Reactjs,Ecmascript 6,Javascript Objects,如何在对象中的数组中查找属性并将这些对象移动到对象中的另一个数组?如何从this.state.comments中提取{“comment”:“value”},并将其放在comments数组中comment对象中其他对象之后? 我重新映射了数组并提取了值本身-注释属性。如何提取整个对象,使其看起来像{“comment:”value} 预期效果: const comment = { "comments": [{'comment': 'aaa'}, {'comment': 'bbb'},

如何在对象中的数组中查找属性并将这些对象移动到对象中的另一个数组?如何从
this.state.comments
中提取{“comment”:“value”},并将其放在
comments
数组中
comment
对象中其他对象之后? 我重新映射了数组并提取了值本身-
注释属性
。如何提取整个对象,使其看起来像{“comment:”value}

预期效果:

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}, 
           {"comment": "eeeee"}, {"comment": "rrrrr"}, {"comment": 
           "ggggg"}, "comment": "hhhhhh"]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
}

只需使用
forEach
遍历每个项目,并检查键是否为
comment
——如果是,则推到
comments
数组

const comment={“comments”:[{'comment':'aaa'},{'comment':'bbb'}],“description”:“yty”,“id”:3,“title”:“gfgf”};
const state={comments:[{“comment”:“eeeee”},{“comment”:“rrrrrrr”},{“comment”:“ggggg”},{“date”:“Thu Jun 13 2019 01:27:09”,“desc”:“dfdfdff”,“comment”:“hhhhhhhh”};
state.comments.forEach({comment:c})=>c?comment.comments.push({c}):c);
console.log(注释);

.as console wrapper{max height:100%!important;top:auto;}
只需使用
forEach
遍历每个项目,并检查键是否为
comment
-如果是,请按到
comments
数组

const comment={“comments”:[{'comment':'aaa'},{'comment':'bbb'}],“description”:“yty”,“id”:3,“title”:“gfgf”};
const state={comments:[{“comment”:“eeeee”},{“comment”:“rrrrrrr”},{“comment”:“ggggg”},{“date”:“Thu Jun 13 2019 01:27:09”,“desc”:“dfdfdff”,“comment”:“hhhhhhhh”};
state.comments.forEach({comment:c})=>c?comment.comments.push({c}):c);
console.log(注释);
.as控制台包装{最大高度:100%!重要;顶部:自动;}
const注释={
注释:[{comment:'aaa'},{comment:'bbb'}],
描述:“YTY”,
id:3,
标题:“GFGF”
}
常数新成员=[
{评论:“eeeee”},
{注释:“RRR”},
{评论:“ggggg”},
{日期:“2019年6月13日星期四01:27:09”,
描述:“DFDF”,
评论:“hhhhh”
}
];
comment.comments=newComments.reduce((res,obj)=>obj.comment?[…res,{comment:obj.comment}]:res,comment.comments | |[]))
console.log(注释);
const注释={
注释:[{comment:'aaa'},{comment:'bbb'}],
描述:“YTY”,
id:3,
标题:“GFGF”
}
常数新成员=[
{评论:“eeeee”},
{注释:“RRR”},
{评论:“ggggg”},
{日期:“2019年6月13日星期四01:27:09”,
描述:“DFDF”,
评论:“hhhhh”
}
];
comment.comments=newComments.reduce((res,obj)=>obj.comment?[…res,{comment:obj.comment}]:res,comment.comments | |[]))
console.log(注释);
const注释={
“评论”:[{'comment':'aaa'},{'comment':'bbb'}],
“说明”:“YTY”,
“id”:3,
“标题”:“GFGF”
}
让新来者=[
{“评论”:“eeeee”},
{“评论”:“rrrrr”},
{“评论”:“ggggg”},
{“日期”:“2019年6月13日星期四01:27:09”,
“描述”:“dfdfdf”,
“评论”:“hhhhhh”
}
]
新成员。forEach(t=>{
如果(t.comment)comment.comments.push({
评论:t.comment
})
});
console.log(注释);
const注释={
“评论”:[{'comment':'aaa'},{'comment':'bbb'}],
“说明”:“YTY”,
“id”:3,
“标题”:“GFGF”
}
让新来者=[
{“评论”:“eeeee”},
{“评论”:“rrrrr”},
{“评论”:“ggggg”},
{“日期”:“2019年6月13日星期四01:27:09”,
“描述”:“dfdfdf”,
“评论”:“hhhhhh”
}
]
新成员。forEach(t=>{
如果(t.comment)comment.comments.push({
评论:t.comment
})
});

console.log(注释);
我想在react中使用它,但时间加倍。我想在react中使用它,但时间加倍。我想在react中使用它,但时间加倍。//stackblitz.com/edit/react-jfkwnuI想在react中使用它,但时间加倍。//stackblitz.com/edit/react-jfkwnu
const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}, 
           {"comment": "eeeee"}, {"comment": "rrrrr"}, {"comment": 
           "ggggg"}, "comment": "hhhhhh"]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
}