q150509-3
by jun68ykt
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
JavaScript
// 更新前のオブジェクト
const objectA = {
id: 1,
description: "製品Aです。",
images: [
"1.jpg",
"2.jpg",
"3.jpg",
],
};
// 更新後のオブジェクト
const objectB = {
id: 1,
description: "製品Aです。",
images: [
"1.jpg",
"2.jpg",
"3.jpg",
],
};
// 更新されたプロパティの配列を得る。(https://stackoverflow.com/a/31686152)
const diffProps = _.reduce(objectA, function(result, value, key) {
return _.isEqual(value, objectB[key]) ?
result : result.concat(key);
}, []);
console.log(diffProps);
// 更新されたプロパティの値のみを持つオブジェクトを作成
const diffObj = diffProps.reduce((obj, prop) => {
obj[prop] = objectB[prop];
return obj;
}, {});
console.log(diffObj);