简体   繁体   English

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

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

I have an array of Objects named results and have the Object which key is the key value of editKeyValues object and value needs to be add in edit key.我有一个名为 results 的对象数组,并且有 Object 哪个键是 editKeyValues object 的键值,需要在编辑键中添加值。 Below is the Input下面是输入

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 that i want in a below format我想要以下格式的 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
        }
      ]
    },
  ]
},]}]

Provide me a best approach because there is large amount of data in results Object... Is recursion a good idea???给我一个最好的方法,因为结果中有大量数据 Object ...递归是个好主意吗??? Help me to find out the best approach.帮我找出最好的方法。

 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));

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: '2常量数据= [ { 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, 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', }, ], }, ]; 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));

You could take a Map and iterate the objects and seach for nested array.您可以使用Map并迭代对象并搜索嵌套数组。

This approach uses a short circuit if no more nodes are eligible for update.如果没有更多节点符合更新条件,则此方法使用短路。

 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.

相关问题 如何根据另一个键/值对数组过滤具有复杂嵌套对象的数组? - How to filter an array with complex nested objects based on another array of key/value pairs? 键对象数组通过object键嵌套在另一个object里面的另一个数组? - Key array of objects by object key nested in another object inside another array? 如何向对象添加新的对象数组以使其嵌套,如果新添加的数组已存在,则向该数组添加另一个对象? - How to add a new array of objects to an object to make it nested and if newly added array already exists, add another object to that array? 对于每个 object,如何将特定键值从一个 object 添加到另一个对象数组 - How to add specific key values from one object to another array of objects, for each object 如何基于另一个键值将新的键值对添加到数组中的对象 - How to add new key value pair to an object in an array based on another key-value 将数组转为对象,并从另一个 object 添加键值 - Turn array into objects, and add key values from another object 如何使用Ramda基于嵌套键对对象数组进行排序? - How to sort an array of objects based on nested key using Ramda? 如何根据每个对象id为数组中的每个对象添加一个密钥对值? - How to add to each object in an array a key-pair value based on each objects id? 如何遍历对象数组并根据 JavaScript 中的条件添加新的对象键? - How to loop through array of objects and add new object key based on condition in JavaScript? 基于嵌套的object过滤嵌套对象数组 - Filter array of nested objects based on nested object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM