繁体   English   中英

如何将对象附加到现有的 json 对象数组中

[英]How to append the object into existing json array of objects

我有像下面这样的 json 对象,它将是动态的,

let data_existing= [
       {
          "client":[
             {
                "name":"aaaa",
                "filter":{
                   "name":"123456"
                }
             }
          ]
       },
       {
          "server":[
             {
                "name":"qqqqq",
                "filter":{
                   "name":"984567"
                }
             }
          ]
       },
               ]

从输入中,我将得到一个如下所示的对象,

let data_new =  {
      "client":[
         {
            "name":"bbbbb",
            "filter":{
               "name":"456789"
            }
         }
      ]
    }

我需要将此对象附加到现有的“客户端”json 对象中。 预期的输出将是这样的,

[
   {
      "client":[
         {
            "name":"aaaa",
            "filter":{
               "name":"123456"
            }
         },
         {
            "name":"bbbb",
            "filter":{
               "name":"456789"
            }
         }
      ]
   },
   {
      "server":[
         {
            "name":"qqqqq",
            "filter":{
               "name":"984567"
            }
         }
      ]
   }
]

并且,如果主对象中不存在“data_new”,则应将其作为如下新对象,例如,

 let data_new =  {
          "server2":[
             {
                "name":"kkkkk",
                "filter":{
                   "name":"111111"
                }
             }
          ]
        }

output will be like,
[
   {
      "client":[
         {
            "name":"aaaa",
            "filter":{
               "name":"123456"
            }
         },
      ]
   },
   {
      "server":[
         {
            "name":"qqqqq",
            "filter":{
               "name":"984567"
            }
         }
      ]
   },
{
      "server2":[
         {
            "name":"kkkkk",
            "filter":{
               "name":"11111"
            }
         }
      ]
   }
]

我尝试了以下方法,但没有按预期工作。 一些帮助将不胜感激。

像下面一样尝试但没有按预期工作,

function addData(oldData, newData) {
  let [key, value] = Object.entries(newData)[0]
  return oldData.reduce((op, inp) => {
    if (inp.hasOwnProperty(key)) {
    console.log("111");
      op[key] = inp[key].concat(newData[key]);
    } else {
    console.log(JSON.stringify(inp));
      op = Object.assign(op, inp);
    }
    return op
  }, {})
}

key已经属于data_existing (例如:“client”)时,您的功能似乎可以工作。

但是你必须处理第二个用例:当key中的对象没有被发现data_existing (如:“服务器2”)。
这将在reduce循环之后执行,如果未找到键,则将新项目添加到data_existing

以下是您如何实现这一目标的示例:

function addData(inputData, inputItem) {
  const [newKey, newValue] = Object.entries(inputItem)[0];

  let wasFound = false; // true iif the key was found in list
  const res = inputData.reduce((accumulator, item) => {
    const [key, value] = Object.entries(item)[0];
    const keyMatch = key === newKey;
    if (keyMatch) {
      wasFound = true;
    }
    // concatenate the lists in case of key matching
    const newItem = { [key]: keyMatch ? [...value, ...newValue] : value };
    return [...accumulator, newItem];
  }, []);

  if (!wasFound) {
    res.push(inputItem); // if key was not found, add item to the list
  }
  return res;
}

希望能帮助到你。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM