简体   繁体   English

如何将 object 插入到 object 内的数组中?

[英]How to insert an object into an array which is inside an object?

Suppose if we are having data like this, how can we use dot notations or anything to update the array(data2) inside the object data1 which is inside the array(data).假设如果我们有这样的数据,我们如何使用点符号或任何东西来更新数组(数据)内的 object 数据 1 内的数组(数据 2)。

data = [
data1:
    {
        a:"1",
        b:"2"
    }
]

    // another array(data2 - below) I am having I have to push that array inside 
    data1 as an object
data2 = [
    {
        c:"3"
    },
    {
        d:"4"
    }
    ]

The response I want as below:我想要的回复如下:

    data = [
data1:
    {
        a:"1",
        b:"2"
    },
data2 = [
    {
        c:"3"
    },
    {
        d:"4"
    }
  ]
]

I didnt really understand your question but i think this is what you want?!我真的不明白你的问题,但我认为这就是你想要的?!

data = {
    data1:
    {
        a: "1",
        b: "2"
    }
}
data2 = [
    {
        c: "3"
    },
    {
        a: "3"
    },
    {
        d: "4"
    }
]

for (var key in data2) {
    for (var key2 in data2[key]) {
        if(data.data1[key2] != null){
            console.log("data.data1 with they key" + key2 + " could have been replaced/updated with " + data2[key][key2]);
        }
        console.log("key " + key2 + " has value " + data2[key][key2]);
    }
}

Result:结果:

key c has value 3 data.data1 with they keya could have been replaced/updated with 3 key a has value 3 key d has value 4键 c 具有值 3 data.data1 与它们 keya 可能已被替换/更新为 3 键 a 具有值 3 键 d 具有值 4

Edit:编辑:

Why dont you just do你为什么不做

data["data2"] = data2?

var array = [];

array['data1'] = { 'name': 'a' }

var array2 = [{ c: 3 }, { d: 4 }];

array['data2'] = array2;

console.log(array)

OutPut: OutPut:

[ data1: { name: 'a' }, data2: [ { c: 3 }, { d: 4 } ] ] [数据1:{名称:'a'},数据2:[{c:3},{d:4}]]

I tried on myself and I got the solution.我自己试了一下,我得到了解决方案。 If we have to make another field in an object you just give a name like this如果我们必须在 object 中创建另一个字段,您只需给出这样的名称

data.dataTwo

this will create another field with new name and now if we want to transfer or store an array in this we can simply do this这将创建另一个具有新名称的字段,现在如果我们想在其中传输或存储一个数组,我们可以简单地执行此操作

data.dataTwo = data2

This will assign that data2 named array in a new field what is given as dataTwo这将在一个新字段中分配名为 data2 的数组,该字段以 dataTwo 的形式给出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 object 的特定数组中插入一个值 - How to insert a value inside a particular array of object 如何在数组中插入 label 的 object 和值 - How to insert object of label and value inside array 如何在 Z0ECD11C1D7A287401D148A23BBD7A2 数组中的 object 中获取 JSON object - How to get JSON object inside an object which is inside JSON array 如何获取对象内部的数组大小? - How to get array size which is inside an object? 如何获取JavaScript数组对象内部的对象? - How to get object which is inside array object in javascript? 我如何将 object 推入 object 内的数组,该数组也在另一个数组内 - How do i push an object into an array which is inside an object which is also inside an another array 如何将一个数组推入另一个数组内部的对象中? - How to push an array into an Object which is inside of another array? Reach 数组中 object 中的元素是数组中 object 中的元素 - Reach element in an object inside an array which is an element in an object inside an array 如何访问对象数组内的对象,该对象数组在JavaScript中的另一个数组内? - how to access object inside an object Array which is inside another array in javascript? 删除数组内的对象属性,数组内的对象属性,不在数组上循环 - Remove object property which inside an array which inside an object which inside an array without looping on array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM