繁体   English   中英

如何将多个对象依次推入一个对象

[英]how to push multiple objects into one object serially

我在一个变量中有2个或多个对象,我想将这些对象推入一个对象。

let a = {"device_type":"iphone","filter_data":{"title":{"value":"Lorem Ipsum..","data":{}},"message":{"value":"Lorem Ipsum is simply dummy text of the printing...","data":{}},"dismiss_button":{"value":"Ok","data":{}},"action_url":{"value":"","data":{"type":"custom"}}}}

{"device_type":"iphone","filter_data":{"message":{"value":"Push Message goes here.","data":{}}}}

我希望输出为:

{
  "0": {
    "device_type": "iphone",
    "filter_data": {
      "title": {
        "value": "Lorem Ipsum..",
        "data": {}
      },
      "message": {
        "value": "Lorem Ipsum is simply dummy text of the printing...",
        "data": {}
      },
      "dismiss_button": {
        "value": "Ok",
        "data": {}
      },
      "action_url": {
        "value": "",
        "data": {
          "type": "custom"
        }
      }
    }
  },
  "1": {
    "device_type": "iphone",
    "filter_data": {
      "message": {
        "value": "Push Message goes here.",
        "data": {}
      }
    }
  }
}

我怎样才能做到这一点?

您可以将}{替换为},{ ,对其进行解析,并采用Object.assign从数组中获取具有索引作为属性的对象。

 const data = '{"device_type":"iphone","filter_data":{"title":{"value":"Lorem Ipsum..","data":{}},"message":{"value":"Lorem Ipsum is simply dummy text of the printing...","data":{}},"dismiss_button":{"value":"Ok","data":{}},"action_url":{"value":"","data":{"type":"custom"}}}}{"device_type":"iphone","filter_data":{"message":{"value":"Push Message goes here.","data":{}}}}'; result = Object.assign({}, JSON.parse(`[${data.replace(/\\}\\{/g, '},{')}]`)); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

如果它们在数组中,则非常简单-只需使用reduce

 const data = [{"device_type":"iphone","filter_data":{"title":{"value":"Lorem Ipsum..","data":{}},"message":{"value":"Lorem Ipsum is simply dummy text of the printing...","data":{}},"dismiss_button":{"value":"Ok","data":{}},"action_url":{"value":"","data":{"type":"custom"}}}},{"device_type":"iphone","filter_data":{"message":{"value":"Push Message goes here.","data":{}}}}]; const res = data.reduce((a, c, i) => (a[i] = c, a), {}); console.log(res); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

您可以使用Array.protoype.match分隔每个对象,然后使用Array.protoype.reduce获得预期的对象。

 let a = '{"device_type":"iphone","filter_data":{"title":{"value":"Lorem Ipsum..","data":{}},"message":{"value":"Lorem Ipsum is simply dummy text of the printing...","data":{}},"dismiss_button":{"value":"Ok","data":{}},"action_url":{"value":"","data":{"type":"custom"}}}}{"device_type":"iphone","filter_data":{"message":{"value":"Push Message goes here.","data":{}}}}'; let objects = a.match(/({"device_type".*?}}}})/g).map(e => JSON.parse(e)); console.log('Array of objects',objects) const out = {...[objects]}; console.log('\\ndesired output',out) 

同样,当键只是索引时,将数组转换为对象似乎没有用。

暂无
暂无

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

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