如何将新数组添加到另一个数组的特定对象中
How to add new array into specific object of another array
我试图找到答案但没有成功。
json 数据的示例,假设它将被命名为 mainArray:
[{
"id": 4455,
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}]
数据嵌套数组的示例需要添加到 mainArray 中:
[{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}]
我们必须检查 "id" 键(在 mainArray 中)是否等于 "specificId" 变量:
var specificId = 4455;
如果它们相等 — 我们需要将 nestedArray 作为带有对象列表的新键添加到 mainArray 的对象中,因此返回的结果应该是这样的:
[{
"id": 4455,
"newKey": [{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}];
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}];
以下是 Plunker 上的代码示例:https://plnkr.co/edit/YJ3lTbleKrBJBaJm7VwU?p=preview
我有一种感觉,应该不难,但我在解决这项任务的 angularjs 方面经验不足。提前感谢您的回答!
更新
更新了 Plunker 中的代码,其中包含 Nina Scholz 的答案(链接在上面)
以及来自合盛的另一种解决方案
http://jsbin.com/xaxagusuma/edit?html,js,console,output
感谢大家的帮助!
在普通的Javascript中,你可以使用Array.prototype.some()
进行迭代,因为可能会短路。
some()
方法测试数组中的某个元素是否通过了所提供函数实现的测试。
var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }],
nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }],
specificId = 4455;
mainArray.some(function (a) {
if (a.id === specificId) {
a.newKey = nastedArray;
return true;
}
});
document.write('<pre>' + JSON.stringify(mainArray, 0, 4) + '</pre>');
Array.prototype.find
的替代方案
find() 方法返回数组中的值,如果数组中的元素满足提供的测试函数。否则返回未定义。
(看看兼容性表!
var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }],
nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }],
specificId = 4455;
var item = mainArray.find(x => x.id === specificId);
if (item !== undefined) {
item.newKey = nastedArray;
}
console.log(JSON.stringify(mainArray));
正如你怀疑的那样,这并不难。最困难的部分是找到正确的对象来插入 nastedArray。
var mainArray = [/**/];
var nastedArray = [/***/];
var index = null;
var specificId = 4455;
for (var i = 0; index === null, i < mainArray.length; i += 1) {
if (mainArray[i].id === specificId) {
index = i;
}
}
if (index !== null) {
mainArray[index].newKey = nastedArray;
}
保持简单怎么样:)
for (var i = 0, i < mainArray.length; i++) {
if (mainArray[i].id === specificId) {
mainArray[i].newKey = nestedArray;
break;
}
}
适用于所有浏览器。
相关文章:
- JavaScript数组包含一个值
- 使用事件更改数组的一个元素
- 从 javascript 数组创建一个 Jquery 数组
- 使用javascript匹配字符串中数组的一个元素
- Angular2从数组创建一个列表
- 在Javascript数组或一个对象中存储多个数据所需的Tweak
- 你能给被劫持的JavaScript数组添加一个函数吗
- 从带有循环的数组中一个接一个地在画布上绘制形状
- 将图像数组从一个窗口传递到另一个窗口,并在javascript的新窗口中显示图像
- 使用拆分字符串的数组创建一个对象
- 多维数组 - 创建一个表
- 将数组从一个函数传递到另一个函数
- 如何将复选框值的数组从一个JSP页面传递到另一个页面
- 使用Knockout.Js将数组绑定到表,并将数组的一个项绑定到另一个元素
- 需要访问数组从一个js到另一个
- 我如何发送数据像数组从一个html文件到另一个html页面
- 从另一个对象和要保存的键数组生成一个对象的最简洁的方法
- 用另一个js数组填充一个javascript数组
- 将对象数组从一个函数传递到另一个函数
- 根据另一个值数组过滤一个对象数组,返回一个空列表