繁体   English   中英

如何在基于另一个 object 的嵌套对象数组中添加键

[英]How to add key in a nested array of Objects based on another object

我有一个名为 results 的对象数组,并且有 Object 哪个键是 editKeyValues object 的键值,需要在编辑键中添加值。 下面是输入

var result =  [{
  name  :  'database',
  checked : true,
  key :  1,
  schemas  : [
    {
      name  : "schema2",
      checked : true,
      key : 6,
      tables : [
        {
          name  : "table2",
          checked : true,
          key : 7,
          columns : [
            {
              name  : "column1",
              checked : true,
              key : 8,
            }
          ]
        },
      ]
    },

  ]
}]

var editKeyValues = { 8 : "column4", 6 : "schema4"}

我想要以下格式的 Output

    var result =  [{
      name  :  'database',
      checked : true,
      key :  1,
      schemas  : [
{
  name  : "schema2",
  checked : true,
  key : 6,
  edit : "schema4"
  tables : [
    {
      name  : "table2",
      checked : true,
      key : 7,
      columns : [
        {
          name  : "column1",
          checked : true,
          key : 8,
          edit : "column4"     // value of key 8 from editkeyValues array
        }
      ]
    },
  ]
},]}]

给我一个最好的方法,因为结果中有大量数据 Object ...递归是个好主意吗??? 帮我找出最好的方法。

 const data = [ { id: 13, product_name: 'Onion Pizza', image: '26238.jpg', category: { id: 1, name: 'Restaurants', image: 'restaurant.png', created_at: '2022-02-02T07:59:05.000000Z', updated_at: '2022-02-15T10:58:49.000000Z', }, status: '1', created_at: '2022-02-19T12:22:09.000000Z', updated_at: '2022-03-02T09:43:53.000000Z', add_in_cart: false, quantity_in_cart: 0, add_in_wishlist: false, variant: [ { id: 34, price: 140, quantity: 10, weight: '7 inch', discount: 20, product_id: 13, created_at: '2022-03-02T12:46:06.000000Z', updated_at: '2022-03-02T12:46:06.000000Z', }, { id: 35, price: 350, quantity: 5, weight: '14 inch', discount: 20, product_id: 13, created_at: '2022-03-02T12:46:06.000000Z', updated_at: '2022-03-02T12:46:06.000000Z', }, ], }, { id: 15, product_name: 'Veg Fresh Pizza', image: '816404.jpg', category: { id: 1, name: 'Restaurants', image: 'restaurant.png', created_at: '2022-02-02T07:59:05.000000Z', updated_at: '2022-02-15T10:58:49.000000Z', }, status: '1', created_at: '2022-03-01T12:40:15.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', add_in_cart: false, quantity_in_cart: 0, add_in_wishlist: false, variant: [ { id: 30, price: 129, quantity: 1, weight: '7 inch', discount: 20, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, { id: 31, price: 200, quantity: 1, weight: '12 inch', discount: 25, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, { id: 32, price: 500, quantity: 1, weight: '18 inch', discount: 40, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, { id: 33, price: 700, quantity: 1, weight: '20 inch', discount: 30, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, ], }, ]; let i = 0; let j = 0; for (i; i < data.length; i++) { for (j; j < data[i].variant.length; j++) { data[i].variant.forEach(e => { e.myKey = 0; }); } } console.log('data: ' + JSON.stringify(data));

常量数据= [ { id: 13, product_name: 'Onion Pizza', image: '26238.jpg', category: { id: 1, name: 'Restaurants', image: 'restaurant.png', created_at: '2022- 02-02T07:59:05.000000Z', updated_at: '2022-02-15T10:58:49.000000Z', }, status: '1', created_at: '2022-02-19T12:22:09.000000Z', updated_at: '2022-03-02T09:43:53.000000Z', add_in_cart: false, quantity_in_cart: 0, add_in_wishlist: false, variant: [ { id: 34, price: 140, quantity: 10, weight: '7 inch', discount: 20, product_id: 13, created_at: '2022-03-02T12:46:06.000000Z', updated_at: '2022-03-02T12:46:06.000000Z', }, { id: 35, 价格: 350, 数量: 5 , weight: '14 inch', discount: 20, product_id: 13, created_at: '2022-03-02T12:46:06.000000Z', updated_at: '2022-03-02T12:46:06.000000Z', }, ], }, { id: 15, product_name: 'Veg Fresh Pizza', image: '816404.jpg', category: { id: 1, name: '餐厅', image: 'restaurant.png', created_at: '2022-02 -02T07:59:05.000000Z', updated_at: '2022-02-15T10:58:49.000000Z', }, status: '1', created_at: '2 022-03-01T12:40:15.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', add_in_cart: false, quantity_in_cart: 0, add_in_wishlist: false, variant: [ { id: 30, price: 129 ,数量:1,重量:'7 英寸',折扣:20,product_id:15,created_at:'2022-03-02T12:27:28.000000Z',updated_at:'2022-03-02T12:27:28.000000Z', }, { id: 31, price: 200, quantity: 1, weight: '12 inch', discount: 25, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022- 03-02T12:27:28.000000Z', }, { id: 32, price: 500, quantity: 1, weight: '18 inch', discount: 40, product_id: 15, created_at: '2022-03-02T12:27 :28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, { id: 33, price: 700, quantity: 1, weight: '20 inch', discount: 30, product_id: 15, created_at: '2022-03-02T12:27:28.000000Z', updated_at: '2022-03-02T12:27:28.000000Z', }, ], }, ];

    let i = 0;
    let j = 0;
    
    for (i; i < data.length; i++) {
      for (j; j < data[i].variant.length; j++) {
        data[i].variant.forEach(e => {
          e.myKey = 0;
        });
      }
    }
    console.log('data : ' + JSON.stringify(data));

您可以使用Map并迭代对象并搜索嵌套数组。

如果没有更多节点符合更新条件,则此方法使用短路。

 function update(array, nodes) { return array.some(o => { var key = o.key.toString(); if (nodes.has(key)) { o.edit = nodes.get(key); nodes.delete(key); if (!nodes.size) return true; } return update(Object.values(o).find(Array.isArray) || [], nodes); }); } var data = [{ name: 'database', checked: true, key: 1, schemas: [{ name: "schema1", checked: true, key: 2, tables: [{ name: "table1", checked: true, key: 3, columns: [{ name: "column2", checked: true, key: 5 }] }] }, { name: "schema2", checked: true, key: 6, tables: [{ name: "table2", checked: true, key: 7, columns: [{ name: "column1", checked: true, key: 8 }] }] }] }], keyValues = { 8: 'column4', 6: 'schema4' }; update(data, new Map(Object.entries(keyValues))); console.log(data);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

 let data = [{ name: 'database', checked: true, key: 1, schemas: [ { name: "schema2", checked: true, key: 6, tables: [ { name: "table2", checked: true, key: 7, columns: [ { name: "column1", checked: true, key: 8, } ] }, ] }, ] }]; let schemas = data.flatMap(d => d.schemas); let tables = schemas.flatMap(s => s.tables); let columns = tables.flatMap(t => t.columns); let flatData = [data, schemas, tables, columns].flat(); let editKeyValues = {8: "column4", 6: "schema4"}; Object.entries(editKeyValues).forEach(([key, newName]) => flatData.find(obj => obj.key === parseInt(key)).edit = newName); console.log(data);

暂无
暂无

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

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