简体   繁体   中英

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. 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

    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??? 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 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', }, ], }, ];

    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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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