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

时间:2020-03-30 07:07:07

标签: javascript json

我有一个像下面这样的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
  }, {})
}

1 个答案:

答案 0 :(得分:0)

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

但是您必须处理第二个用例:当在key的对象中找不到data_existing时(例如:“ server2”)。
此操作应在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;
}

希望有帮助。