我有两个带有对象的数组,我想比较它们的值并返回更改键值pait和数组位置 [英] I have two arrays with objects inside that I want to compare the values and return the change key value pait and array position
问题描述
我想比较这两个arryays并输出更改后的值的键值对,因此输出应该像这样,以便只有更改后的对象位于新数组中,如果还有其他更改后的对象,它们将按顺序排列有改变的领域.我一直希望col10 ID出现.
I want to compare these two arryays and output the key value pairs of the changed value so the out put should be like so only the changed object is in a new array, and if there were other changed objects they would be in order with there changed fields. and I always want the col10 id to be present.
提前谢谢
我尝试了以下
oldState.map((item, i) => {
saveData.map((item1, j) => {
if (item[`col${i}`] === item1[`col${j}`]) {
console.log(item1);
}
});
});
两个数组比较的理想结果
desired result of comaparison of two 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"
}
]
尝试答案后我会得到什么
What i get after trying the answer
推荐答案
我映射了 oldState
和 saveData
数组以返回数组 [old,new]
.然后创建函数以返回具有不同值的键和值对
I mapped the the oldState
and saveData
arrays to return an array [old, new]
for each pair.
Then created the function to return key and value pair with the different values
提供的两个版本的对象都具有相同的键.
Provided both versions of the object have the same keys.
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)));
这篇关于我有两个带有对象的数组,我想比较它们的值并返回更改键值pait和数组位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!