【问题标题】:How to restructure the JSON Object with new key values?如何用新的键值重构 JSON 对象?
【发布时间】:2021-10-08 06:24:29
【问题描述】:

请帮助我重新创建从 API 接收的 JSON 对象,请参阅下面的来自 API 的 JSON 对象 下面是输入 JSON

{
  'Demo-1': [
    {
      sku: 'Brisk',
      count: '2',
    },
    {
      sku: 'Pepsi Cans',
      count: '2',
    },
    {
      sku: 'Pepsi Cans',
      count: '4',
    }
    
  ],
  
  'Demo-2' : [
    {
      sku: 'Mountain',
      count: '4',
    },
    {
      sku: 'Pepsi Bottles',
      count: '4',
    },
    {
      sku: 'Lipton Dietgreentea',
      count: '2',
    },
    {
      sku: 'Lipton Dietgreentea Mixedberry',
      count: '2',
    }
  ]
}

由此转换下面的带有附加键的 JSON 对象,并在另一个键内重构数组项

结果应该是这样的

{
    'Demo-1': {
        items: [{
                sku: 'Brisk',
                count: '2',
            },
            {
                sku: 'Pepsi',
                count: '2',
            },
            {
                sku: 'Pepsi',
                count: '4',
            }

        ],
        mode: "Veri",
        status: "open"

    },
    'Demo-2': {
        "items": [{
                sku: 'Mountain',
                count: '4',
            },
            {
                sku: 'Pepsi Bottles',
                count: '4',
            },
            {
                sku: 'Lipton Dietgreentea',
                count: '2',
            },
            {
                sku: 'Lipton Dietgreentea Mixedberry',
                count: '2',
            }
        ],
        mode: "Clear",
        status: "Closed"
    }
}

我尝试了 Angular 但我无法达到结果,请查看下面的代码

 Object.entries(_data).map((items,idx)=>{
     
      this._newPickLists = {
        items[0] :{
          items : items[1],
          mode : 'Verification',
          status : 'open'
        }
      }

    
    })

提前致谢

【问题讨论】:

  • 你能提供一个代码sn-p吗?像this 这样的东西没有意义,我们无法重现问题
  • 这只是 angular _newPickLists: any; 中的一个声明;

标签: javascript arrays json


【解决方案1】:

使用Object.entriesObject.fromEntries,您可以将值更改为以项目为键的对象

const obj = {"Demo-1":[{"sku":"Brisk","count":"2"},{"sku":"Pepsi Cans","count":"2"},{"sku":"Pepsi Cans","count":"4"}],"Demo-2":[{"sku":"Mountain","count":"4"},{"sku":"Pepsi Bottles","count":"4"},{"sku":"Lipton Dietgreentea","count":"2"},{"sku":"Lipton Dietgreentea Mixedberry","count":"2"}]};

console.log(
  Object.fromEntries(
    Object.entries(obj).map(
      ([key, items]) => [key, { items, mode: "verification", status: "open" }]
    )
  )
)

【讨论】:

    【解决方案2】:

    只需将newObject 替换为您应用中的变量即可。

    const newObject = {};
    
    for (const [key, value] of Object.entries(obj)) {
        newObject[key] = {};
        newObject[key]["items"] = value;
        newObject[key].mode = "Clear";
        newObject[key].status = "Closed";
    
    }
    
    console.log(newObject);
    
    
    

    【讨论】:

      【解决方案3】:

      这是一个将更新原始数据对象的解决方案:

        Object.keys(data).forEach((key)=> { 
           data[key] = {items: data[key], mode: 'Veri', status: 'open' }}
        )
      

      【讨论】:

        猜你喜欢
        • 2021-12-14
        • 2019-11-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 2018-07-28
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多