我用样本解释了这个问题。
例如我有javascript对象。它看起来像:
var trial= { points:[{x:1,y:2},{x:5,y:2},{x:3,y:4}] , obj:{id:5,name:"MyName"} }
我使用深度diff模块来找出两个json数组之间的差异。然后找到差异并找到差异路径。如果x的值改变了,那么它会找到。
例如
path = ["points",0,"x"]
or
path= ["obj","name"]
所以我的问题是如何从这些路径生成json对象。
例如,我必须生成
trial.points[0].x or trial.obj.name
怎么做?谢谢你的回答。
答案 0 :(得分:3)
您可以使用以下逻辑:
spawn bash -c "ssh root@192.168.1.1 -o StrictHostKeyChecking=no -p 830 -s netconf < $FILE1"
答案 1 :(得分:1)
你可以这样做:
var trial= { points:[{x:1,y:2}, {x:5,y:2},{x:3,y:4}] , obj:{id:5,name:"MyName"}};
var path = ["points", 0, "x"];
var object = trial;
path.map(field => object = object[field]);
console.log(object);
path = ["obj", "name"];
var object = trial;
path.map(field => object = object[field]);
console.log(object);
答案 2 :(得分:1)
您可以使用
var mvxDataConsumer = view?.Tag as IMvxDataConsumer;
并将array#reduce
和object
作为变量传递。 path
将返回与路径对应的值。
Array#reduce
&#13;
答案 3 :(得分:0)
要使用["points", 0, "x"]
等属性名称数组构建对象,请使用良好的JSON Pointer实现,因为大多数实现了翻译这些路径的便捷方法 ,并将值应用于对象图。
例如(nodejs):
const diff = require('deep-diff');
const ptr = require('json-ptr');
let original = {
points: [{ x: 1, y: 2 }, { x: 5, y: 2 }, { x: 3, y: 4 }],
obj: { id: 5, name: "MyName" }
};
let modified = JSON.parse(JSON.stringify(original));
modified.points[0].x = 7;
modified.obj.name = 'Wilbur Finkle';
const differences = diff(original, modified);
// Produce an object that represents the delta between original and modified objects
const delta = differences.reduce((acc, record) => {
// Only process edits and newly added values
if (record.kind === 'E' || record.kind === 'N') {
ptr.set(
acc, // target
ptr.encodePointer(record.path), // pointer; from path
record.rhs, // modified value
true // force; creates object graph
);
}
return acc;
}, {});
console.log(JSON.stringify(delta, null, ' '));
&#13;
制作:
{ points: [ { x: 7 } ], obj: { name: 'Wilbur Finkle' } }