var data =[{
"name": "jack",
"age": 10
}, {
"name": "jhon",
"age": 12
}];
var obje = {"name":{'data_one':0,'data_two':5},"age":{'data_one':8,'data_two':9}};
How can I get the following result from this array and object.
如何从此数组和对象获取以下结果。
data[{"name":{"data_one":0,"data_two":5,"display_name":"jack"}
"age":{"data_one":8,"data_two":9,"display_name":10}},
{"name":{"data_one":0,"data_two":5,"display_name":"jhon"}
"age":{"data_one":8,"data_two":9,"display_name":12}}]
3 个解决方案
#1
0
You could use a nested approach with Object.assign
您可以使用Object.assign的嵌套方法
var data = [{ name: "jack", age: 10 }, { name: "jhon", age: 12 }],
object = { name: { data_one: 0, data_two: 5 }, age: { data_one: 8, data_two: 9 } },
result = data.map(o => Object.keys(o).reduce((r, k) =>
Object.assign(r, {
[k]: Object.assign({}, object[k], { display_name: o[k] })
}), {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
0
Javascript object is a collection of properties, and a property is an association between a name (or key) and a value.
Javascript对象是属性的集合,属性是名称(或键)与值之间的关联。
You can have object like below (remember Key value pair)
您可以拥有如下对象(记住键值对)
spices = [{"name":"pasilla", "spiciness":"mild"},
{"name":"jalapeno", "spiciness":"hot hot hot!"},
{"name":"habanero", "spiciness":"LAVA HOT!!"}];
more details :Javascript object documentation
更多细节:Javascript对象文档
#3
-1
Just use push that is declared in Array prototype.
只需使用在Array原型中声明的push。
data.push(obje)
#1
0
You could use a nested approach with Object.assign
您可以使用Object.assign的嵌套方法
var data = [{ name: "jack", age: 10 }, { name: "jhon", age: 12 }],
object = { name: { data_one: 0, data_two: 5 }, age: { data_one: 8, data_two: 9 } },
result = data.map(o => Object.keys(o).reduce((r, k) =>
Object.assign(r, {
[k]: Object.assign({}, object[k], { display_name: o[k] })
}), {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
0
Javascript object is a collection of properties, and a property is an association between a name (or key) and a value.
Javascript对象是属性的集合,属性是名称(或键)与值之间的关联。
You can have object like below (remember Key value pair)
您可以拥有如下对象(记住键值对)
spices = [{"name":"pasilla", "spiciness":"mild"},
{"name":"jalapeno", "spiciness":"hot hot hot!"},
{"name":"habanero", "spiciness":"LAVA HOT!!"}];
more details :Javascript object documentation
更多细节:Javascript对象文档
#3
-1
Just use push that is declared in Array prototype.
只需使用在Array原型中声明的push。
data.push(obje)