[英]is there any option to get key values separately from a array of objects and make it as separate array of object within single quotes
var Header =[
{
"COL_1": "Name",
"COL_2": "Subject",
"COL_3": "likes",
"COL_4": "city",
},
{
"COL_1": "mohan",
"COL_2": "maths",
"COL_3": "3",
"COL_4": "chennai",
}
];
expected output:预期 output:
var output=[
{
"COL_1":'Name,Subject,Likes,city'
},
{
"COL_1":'mohan,maths,3,chennai'
}
];
Use map
and Object.values
使用
map
和Object.values
var Header = [ { COL_1: "Name", COL_2: "Subject", COL_3: "likes", COL_4: "city", }, { COL_1: "mohan", COL_2: "maths", COL_3: "3", COL_4: "chennai", }, ]; const res = Header.map((obj) => { const [key] = Object.keys(obj); return { [key]: Object.values(obj).join(), }; }); console.log(res)
Can try out this:可以试试这个:
var Header =[ { "COL_1": "Name", "COL_2": "Subject", "COL_3": "likes", "COL_4": "city", }, { "COL_1": "mohan", "COL_2": "maths", "COL_3": "3", "COL_4": "chennai", } ]; let output = []; Header.forEach(elem => { let str = ''; Object.keys(elem).forEach(key => str += elem[key] + ','); output.push({COL_1: str}); }); console.log('output:', output);
Apply the map()
method on the original array.在原始数组上应用
map()
方法。 Then use the Object.keys()
method to get the key for each object, and the Object.values()
method to get the values from the object. Then use the
Object.keys()
method to get the key for each object, and the Object.values()
method to get the values from the object. I would also recommend that you use const or let instead of var for variable declaration.我还建议您使用const或let而不是var进行变量声明。
const Header = [ { "COL_1": "Name", "COL_2": "Subject", "COL_3": "likes", "COL_4": "city", }, { "COL_1": "mohan", "COL_2": "maths", "COL_3": "3", "COL_4": "chennai", } ]; const output = Header.map((el) => { const key = Object.keys(el)[0]; return { [key]: `${Object.values(el)}` } }); console.log(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.