将集合合并到 json 数组
Merging collections to json array
我有2种json格式,即jsonA和jsonB,我想将它们组合成jsonC如下。
如果有人能告诉我该怎么做,我将不胜感激。
json一个
{
"text1": "Hello",
"text2": "Hi",
"text3": "There"
}
jsonB
[
{
"id": "text1",
"category": "Big"
},
{
"id": "text2",
"category": "Medium"
},
{
"id": "text3",
"category": "Small"
},
]
决赛
[
{
"id": "text1",
"category": "Big",
"message": "Hello"
},
{
"id": "text2",
"category": "Medium",
"message": "Hi"
},
{
"id": "text3",
"category": "Small",
"message": "There"
}
]
首先,你需要遍历第二个json数组。比遍历 1st json 并比较索引。如果索引匹配,则将新属性添加到第二个 json.
这里是完整的例子:
var jsonA = '{"text1": "Hello","text2": "Hi","text3": "There" }';
var jsonB = '[{"id": "text1","category": "Big"},{"id": "text1","category": "Medium"},{"id": "text1","category": "Small"}]';
jsonA = JSON.parse(jsonA);
jsonB = JSON.parse(jsonB);
for(var i=0;i<jsonB.length;i++){
for(var j in jsonA){
if(j == ("text"+(i+1))){
jsonB[i].message = jsonA[j];
}
}
}
console.log(JSON.stringify(jsonB));
输出:[{"id":"text1","category":"Big","message":"Hello"},{"id":"text1","category":"Medium","message":"Hi"},{"id":"text1","category":"Small","message":"There"}]
新阵列的解决方案。
基本上它会迭代数组并为每个找到的对象构建一个新对象。它添加了一个新的 属性 message
和想要的内容形式 objectA
.
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }],
objectC = objectB.map(function (a) {
return {
id: a.id,
category: a.category,
message: objectA[a.id]
};
});
document.write('<pre>' + JSON.stringify(objectC, 0, 4) + '</pre>');
突变数组的解决方案。
此解决方案采用数组并在循环中添加一个新的 属性,其值来自 objectA
。
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }];
objectB.forEach(function (a) {
a.message = objectA[a.id];
});
document.write('<pre>' + JSON.stringify(objectB, 0, 4) + '</pre>');
我有2种json格式,即jsonA和jsonB,我想将它们组合成jsonC如下。 如果有人能告诉我该怎么做,我将不胜感激。
json一个
{
"text1": "Hello",
"text2": "Hi",
"text3": "There"
}
jsonB
[
{
"id": "text1",
"category": "Big"
},
{
"id": "text2",
"category": "Medium"
},
{
"id": "text3",
"category": "Small"
},
]
决赛
[
{
"id": "text1",
"category": "Big",
"message": "Hello"
},
{
"id": "text2",
"category": "Medium",
"message": "Hi"
},
{
"id": "text3",
"category": "Small",
"message": "There"
}
]
首先,你需要遍历第二个json数组。比遍历 1st json 并比较索引。如果索引匹配,则将新属性添加到第二个 json.
这里是完整的例子:
var jsonA = '{"text1": "Hello","text2": "Hi","text3": "There" }';
var jsonB = '[{"id": "text1","category": "Big"},{"id": "text1","category": "Medium"},{"id": "text1","category": "Small"}]';
jsonA = JSON.parse(jsonA);
jsonB = JSON.parse(jsonB);
for(var i=0;i<jsonB.length;i++){
for(var j in jsonA){
if(j == ("text"+(i+1))){
jsonB[i].message = jsonA[j];
}
}
}
console.log(JSON.stringify(jsonB));
输出:[{"id":"text1","category":"Big","message":"Hello"},{"id":"text1","category":"Medium","message":"Hi"},{"id":"text1","category":"Small","message":"There"}]
新阵列的解决方案。
基本上它会迭代数组并为每个找到的对象构建一个新对象。它添加了一个新的 属性 message
和想要的内容形式 objectA
.
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }],
objectC = objectB.map(function (a) {
return {
id: a.id,
category: a.category,
message: objectA[a.id]
};
});
document.write('<pre>' + JSON.stringify(objectC, 0, 4) + '</pre>');
突变数组的解决方案。
此解决方案采用数组并在循环中添加一个新的 属性,其值来自 objectA
。
var objectA = { "text1": "Hello", "text2": "Hi", "text3": "There" },
objectB = [{ "id": "text1", "category": "Big" }, { "id": "text2", "category": "Medium" }, { "id": "text3", "category": "Small" }];
objectB.forEach(function (a) {
a.message = objectA[a.id];
});
document.write('<pre>' + JSON.stringify(objectB, 0, 4) + '</pre>');