首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从OBJ-1中读取嵌套的JSON节点/键,并从OBJ-2中找到这些键值,然后在Javascript中更新OBJ-1.JSON

从OBJ-1中读取嵌套的JSON节点/键,并从OBJ-2中找到这些键值,然后在Javascript中更新OBJ-1.JSON
EN

Stack Overflow用户
提问于 2020-07-31 23:42:28
回答 2查看 40关注 0票数 1

让obj1 =obj1.json**

代码语言:javascript
复制
    {
        "stores": {
            "city1": [
                {
                    "address": null,
                    "firstName": null,
                    "lastName": null
                }
            ]
        }
    }

让obj2 = obj2.json**

代码语言:javascript
复制
{
    "stores": {
        "city1": [
            {
                "address": 13, Landiling,
                "firstName": Robot,
                "lastName": Tom,
                "phone": 12345678,
                "email": test@,
                "manager": tim
            }
        ]
    }
}

下面是我使用obj2更新obj1值的代码。

代码语言:javascript
复制
 result = Object.keys(obj1.stores.city1[0]);
    for (var i = 0; i< result.length; i++ ) {
    console.log(result[i]);
    console.log(JSON.stringify(obj1.stores.city1[0].result[i]));
    obj1.stores.city1[0].result[i]=obj2.stores.city1[0].result[i];
    }

我可以使用上面的代码从obj1获得子键,但不能将值更新回obj1。因为它显示为'i‘没有定义或接收到未定义的结果。

EN

回答 2

Stack Overflow用户

发布于 2020-08-01 00:00:59

您需要知道当前的城市关键字,才能在更新对象中查找城市。

  1. 遍历城市密钥
  2. 按城市密钥遍历城市
  3. 在城市数组

中的相应索引处分配更新对象

不确定是否要复制所有属性,但没有示例输出。

代码语言:javascript
复制
const toBeUpdated = {
  "stores": {
    "city1": [{
      "address": null,
      "firstName": null,
      "lastName": null
    }]
  }
}

const updateInfo = {
  "stores": {
    "city1": [{
      "address": "13, Landiling",
      "firstName": "Robot",
      "lastName": "Tom",
      "phone": "12345678",
      "email": "test@",
      "manager": "tim"
    }]
  }
}

Object.keys(toBeUpdated.stores).forEach(cityKey => {
  toBeUpdated.stores[cityKey].forEach((city, index) => {
    Object.assign(city, { ...updateInfo.stores[cityKey][index] });
  });
});

console.log(toBeUpdated);
代码语言:javascript
复制
.as-console-wrapper {
  top: 0;
  max-height: 100% !important;
}

票数 1
EN

Stack Overflow用户

发布于 2020-08-01 00:08:13

我稍微修改了你的代码,让它在我的本地机器上运行。以下是正在实现您想要实现的目标的工作代码。

代码语言:javascript
复制
const obj1 = {
  stores: {
    city1: [
      {
        address: null,
        firstName: null,
        lastName: null
      }
    ]
  }
}
const obj2 = {
  stores: {
    city1: [
      {
        address: '13, Landiling',
        firstName: 'Robot',
        lastName: 'Tom',
        phone: 12345678,
        email: 'test@',
        manager: 'tim'
      }
    ]
  }
}

const result = Object.keys(obj1.stores.city1[0])
console.log('Keys: ', result)

for (var i = 0; i < result.length; i++) {
  console.log('Key: ', result[i])
  console.log('Value:', JSON.stringify(obj1.stores.city1[0][i]))
  obj1.stores.city1[0][result[i]] = obj2.stores.city1[0][result[i]]
}

console.log('object 1: ', JSON.stringify(obj1))
console.log('object 2: ', JSON.stringify(obj2))

下面是您的代码行:

代码语言:javascript
复制
obj1.stores.city1[0].result[i]=obj2.stores.city1[0].result[i];

正被翻译为

代码语言:javascript
复制
obj1.stores.city1[0].result.<value of i> = obj2.stores.city1[0].result.<value of i>;

您正在city1对象上查找不存在的result属性,然后您正在尝试访问result对象上的number属性(其本身是未定义的)。因此,您正在尝试访问未定义对象的未定义属性。这就是您无法更改obj1的原因。我希望我的回答能澄清。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63194992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档