[英]How to filter an array with complex nested objects based on another array of key/value pairs?
[英]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.