[英]Is there any way to append object into the existing array of objects based on the key
[英]Is there any way to replace the existing object inside JSON array with new object based on key
我有以下格式的動態 JSON 數組,
let main_data = [
{
"client":[
{
"name":"aaaa",
"count":"1",
"filter":{
"type":{
"name":"test3"
}
}
},
{
"name":"bbbb",
"count":"9",
"filter":{
"type":{
"name":"test2"
}
}
}
]
},
{
"compute":[
{
"name":"cccc",
"count":"6",
"filter":{
"type":{
"name":"test"
}
}
}
]
}
]
這里的關鍵“名稱”是唯一的。 更新表單時,我會得到一個如下所示的 json 數組,
let new_data = [
{
"client":[
{
"name":"bbbb",
"count":"1234",
"type":{
"name":"updated_name"
}
}
}
]
}
]
我需要檢查“main_data”中json數組中的“name”並刪除現有的並使用新的“updated_data”更新到“main_data”中。 (請不要使用 Jquery)
預期輸出,
let main_data = [
{
"client":[
{
"name":"aaaa",
"count":"1",
"filter":{
"type":{
"name":"test3"
}
}
},
{
"name":"bbbb",
"count":"123",
"filter":{
"type":{
"name":"updated_name"
}
}
}
]
},
{
"compute":[
{
"name":"cccc",
"count":"6",
"filter":{
"type":{
"name":"test"
}
}
}
]
}
]
有什么辦法可以做到這一點。 任何幫助將非常感激。 提前致謝。
嘗試這個
let main_data = [{ client: [{ name: "aaaa", count: "1", filter: { type: { name: "test3" } } }, { name: "bbbb", count: "9", filter: { type: { name: "test2" } } } ] }, { compute: [{ name: "cccc", count: "6", filter: { type: { name: "test" } } }] } ]; let new_data = [{ client: [{ name: "bbbb", count: "1234", filter: { type: { name: "updated_name" } } }] }]; const res = main_data.map((item, index) => { if (item.client) { const clients = item.client.map(client => { if (client.name === new_data[0].client[0].name) { client = new_data[0].client[0]; } return client; }); return { client: clients }; } return item; }); console.log(res);
很可能有一種奇特的方式來完成這項工作,但人們總是可以找到匹配的項目並替換它。 例如。
let main_data = [ { "client": [ { "name": "aaaa", "count": "1", "filter": { "type": { "name": "test3" } } }, { "name": "bbbb", "count": "123", "filter": { "type": { "name": "updated_name" } } } ] }, { "compute": [ { "name": "cccc", "count": "6", "filter": { "type": { "name": "test" } } } ] } ]; let new_data = [ { "client": [ { "name": "bbbb", "count": "1234", "type": { "name": "updated_name" } } ] } ]; console.log("before:" + JSON.stringify(main_data)); newItem = new_data[0]["client"][0]; mainDataList = main_data[0]["client"]; for (i = 0; i < mainDataList.length; i++) { if (mainDataList[i].name == newItem.name) { mainDataList[i] = newItem; } } console.log("after:" + JSON.stringify(main_data));
會輸出
before:[{"client":[{"name":"aaaa","count":"1","filter":{"type":{"name":"test3"}}},{"name":"bbbb","count":"123","filter":{"type":{"name":"updated_name"}}}]},{"compute":[{"name":"cccc","count":"6","filter":{"type":{"name":"test"}}}]}]
after:[{"client":[{"name":"aaaa","count":"1","filter":{"type":{"name":"test3"}}},{"name":"bbbb","count":"1234","type":{"name":"updated_name"}}]},{"compute":[{"name":"cccc","count":"6","filter":{"type":{"name":"test"}}}]}]
這是一個簡單的方法,假設您的新數據位於變量newData
:
main_data.client.filter(item => item.name === newData.name).push(newData)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.