提取对象数组之间的不同道具
Extract the different props between array of objects
实现以下目标的最有效方法是什么:
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, 'id') // [{id: "12", units: 6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 35, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, "id") // [{id: "12", salePrice: 35, units: 6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, "product") // [{product: "6", units:6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let result = extractArrayObjectDifference(oldArray,newArray, "id") // []
extractArrayObjectDifference
函数应该创建一个数组,其中包含包含“id”的对象或作为第三个参数提供的任何其他道具,并且每个道具都是从原始数组中的对象修改而来的。
我尝试了以下方法,但似乎无法正常工作。我使用了 lodash
import isEmpty from 'lodash/isEmpty'
import isEqual from 'lodash/isEqual'
import isObject from 'lodash/isObject'
function difference(object, base) {
return transform(object, (result: any, value, key) => {
if (!isEqual(value, base[key])) {
result[key] = (isObject(value) && isObject(base[key])) ? difference(value, base[key]) : value;
}
});
}
function extractArrayObjectDifference(oldVal, newVal, key) {
let out = [];
for (let i = 0; i <= newVal.length; i++) {
let result = difference(newVal[i], oldVal[i])
if (!isEmpty(result)) {
result[key] = newVal[key]
out.push(result)
}
}
return out
}
提前致谢。
使用Object.entries()
循环旧对象和新对象的属性。然后使用 .filter()
到 select 只是要保留的属性,即两个对象之间不同的属性,以及 id
属性。最后,把它重新组合成一个新对象。
循环数组时,您可以过滤结果以删除只有一个 属性 的对象。那将永远只是 id
属性,所以这意味着所有其他属性都是相同的,您不希望结果中出现这个。
function difference(oldObj, newObj, keyToKeep) {
return Object.fromEntries(
Object.entries(newObj).filter(([key, val]) =>
key == keyToKeep || val != oldObj[key]
)
);
}
function extractArrayObjectDifference(oldArray, newArray, key) {
return oldArray.map((el, i) => difference(el, newArray[i], key))
.filter(obj => Object.keys(obj).length > 1);
}
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, 'id')) // [{id: "12", units: 6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 35, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, "id")) // [{id: "12", salePrice: 35, units: 6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, "product")) // [{product: "6", units:6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
console.log(extractArrayObjectDifference(oldArray, newArray, "id")) // []
实现以下目标的最有效方法是什么:
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, 'id') // [{id: "12", units: 6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 35, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, "id") // [{id: "12", salePrice: 35, units: 6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
let result = extractArrayObjectDifference(oldArray,newArray, "product") // [{product: "6", units:6}]
和
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let result = extractArrayObjectDifference(oldArray,newArray, "id") // []
extractArrayObjectDifference
函数应该创建一个数组,其中包含包含“id”的对象或作为第三个参数提供的任何其他道具,并且每个道具都是从原始数组中的对象修改而来的。
我尝试了以下方法,但似乎无法正常工作。我使用了 lodash
import isEmpty from 'lodash/isEmpty'
import isEqual from 'lodash/isEqual'
import isObject from 'lodash/isObject'
function difference(object, base) {
return transform(object, (result: any, value, key) => {
if (!isEqual(value, base[key])) {
result[key] = (isObject(value) && isObject(base[key])) ? difference(value, base[key]) : value;
}
});
}
function extractArrayObjectDifference(oldVal, newVal, key) {
let out = [];
for (let i = 0; i <= newVal.length; i++) {
let result = difference(newVal[i], oldVal[i])
if (!isEmpty(result)) {
result[key] = newVal[key]
out.push(result)
}
}
return out
}
提前致谢。
使用Object.entries()
循环旧对象和新对象的属性。然后使用 .filter()
到 select 只是要保留的属性,即两个对象之间不同的属性,以及 id
属性。最后,把它重新组合成一个新对象。
循环数组时,您可以过滤结果以删除只有一个 属性 的对象。那将永远只是 id
属性,所以这意味着所有其他属性都是相同的,您不希望结果中出现这个。
function difference(oldObj, newObj, keyToKeep) {
return Object.fromEntries(
Object.entries(newObj).filter(([key, val]) =>
key == keyToKeep || val != oldObj[key]
)
);
}
function extractArrayObjectDifference(oldArray, newArray, key) {
return oldArray.map((el, i) => difference(el, newArray[i], key))
.filter(obj => Object.keys(obj).length > 1);
}
let oldArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
let newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, 'id')) // [{id: "12", units: 6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 35, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, "id")) // [{id: "12", salePrice: 35, units: 6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 6}]
console.log(extractArrayObjectDifference(oldArray, newArray, "product")) // [{product: "6", units:6}]
newArray = [{id: "12", product: "6", price: 30, salePrice: 33, units: 5}]
console.log(extractArrayObjectDifference(oldArray, newArray, "id")) // []