![](/img/trans.png)
[英]I need to compare two arrays and have the output be one array, objects being compared inside
[英]I have two arrays with objects inside that I want to compare the values and return the change key value pait and array position
我想比較這兩個數組和 output 更改值的鍵值對,所以輸出應該是這樣的字段。 我總是希望 col10 id 存在。
提前謝謝
我試過以下
oldState.map((item, i) => {
saveData.map((item1, j) => {
if (item[`col${i}`] === item1[`col${j}`]) {
console.log(item1);
}
});
});
兩個 arrays 比較的期望結果
[
{col0:"snappy", col10:"292959180223939085"}
]
var oldState =
[
{
"col0": "Decor",
"col1": "2021-03-31",
"col2": "okok",
"col3": true,
"col4": 7,
"col5": 5,
"col6": "Curation",
"col7": "fsaf",
"col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615646495/catalog/sse5zxtklsj3ib730zjy.png",
"col9": 4,
"col10": "292959180223939085"
},
{
"col0": "Decor",
"col1": "2021-03-31",
"col2": "fdsafd",
"col3": true,
"col4": 3,
"col5": 3,
"col6": "Curation",
"col7": "fdsfsa",
"col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615657360/catalog/qpudbgkrvftjlo5c1yma.png",
"col9": 5,
"col10": "292970573359743501"
}
]
var saveData =
[
{
"col0": "Snappy",
"col1": "2021-03-31",
"col2": "okok",
"col3": true,
"col4": 7,
"col5": 5,
"col6": "Curation",
"col7": "fsaf",
"col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615646495/catalog/sse5zxtklsj3ib730zjy.png",
"col9": 4,
"col10": "292959180223939085"
},
{
"col0": "Decor",
"col1": "2021-03-31",
"col2": "fdsafd",
"col3": true,
"col4": 3,
"col5": 3,
"col6": "Curation",
"col7": "fdsfsa",
"col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615657360/catalog/qpudbgkrvftjlo5c1yma.png",
"col9": 5,
"col10": "292970573359743501"
}
]
嘗試答案后我得到了什么
我映射了oldState
和saveData
arrays 來為每對返回一個數組[old, new]
。 然后創建 function 以返回具有不同值的鍵值對
前提是 object 的兩個版本具有相同的密鑰。
const oldState = [{ "col0": "Decor", "col1": "2021-03-31", "col2": "okok", "col3": true, "col4": 7, "col5": 5, "col6": "Curation", "col7": "fsaf", "col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615646495/catalog/sse5zxtklsj3ib730zjy.png", "col9": 4, "col10": "292959180223939085" }, { "col0": "Decor", "col1": "2021-03-31", "col2": "fdsafd", "col3": true, "col4": 3, "col5": 3, "col6": "Curation", "col7": "fdsfsa", "col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615657360/catalog/qpudbgkrvftjlo5c1yma.png", "col9": 5, "col10": "292970573359743501" } ] const saveData = [{ "col0": "Snappy", "col1": "2021-03-31", "col2": "okok", "col3": true, "col4": 7, "col5": 5, "col6": "Curation", "col7": "fsaf", "col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615646495/catalog/sse5zxtklsj3ib730zjy.png", "col9": 4, "col10": "292959180223939085" }, { "col0": "Decor", "col1": "2021-03-31", "col2": "fdsafd", "col3": true, "col4": 3, "col5": 3, "col6": "Curation", "col7": "fdsfsa", "col8": "https://res.cloudinary.com/kitson-co/image/upload/v1615657360/catalog/qpudbgkrvftjlo5c1yma.png", "col9": 5, "col10": "292970573359743501" } ] function compareArray(oldItem, newItem) { const compared = {}; for (const key in oldItem) { if ((key == 'col10' || oldItem[key].= newItem[key]) && Object.hasOwnProperty,call(newItem. key) && Object.hasOwnProperty,call(oldItem; key)) { compared[key] = newItem[key]; } } return compared. } oldState,map((old, i) => [old. saveData[i]]).forEach((item) => console.log(compareArray(..;item)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.