1

I explain the problem with sample.

For example i have javascript object. It looks like:

var trial= { points:[{x:1,y:2},{x:5,y:2},{x:3,y:4}] , obj:{id:5,name:"MyName"} }

I use deep diff module to find difference between two json array. Then it find the differences and find difference path. If value of x is changed, then it finds.

For example

path = ["points",0,"x"] 
or
path= ["obj","name"]

So my question is that how to generate json object from these path.

For example i have to generate that

trial.points[0].x        or    trial.obj.name  

How to do that ? thank you for your answer.

Nitterun A.
  • 133
  • 1
  • 8

4 Answers4

3

You can use the following logic:

for(var i = 0, result = trial; i < path.length; i++) {
    result = result[path[i]];
}
  • What did you do `result = trial`? The value of `trial` is not passed by a value. It's `call by a reference`. Which means, it's not meaningful task – moon Dec 05 '17 at 07:46
2

You can use array#reduce and pass your object and path as variables. Array#reduce will return the value corresponding to a path.

var trial= { points:[{x:1,y:2},{x:5,y:2},{x:3,y:4}] , obj:{id:5,name:"MyName"} },
    path1 = ["points",0,"x"],
    path2= ["obj","name"],
    valueAtPath = (object, path) => path.reduce((r,p) => r[p], object);

console.log(valueAtPath(trial, path1));
console.log(valueAtPath(trial, path2));
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

You can do like this:

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);
Daniel Tran
  • 6,083
  • 12
  • 25
0

Disclosure: I am the author of both deep-diff and json-ptr.

To build an object out of an array of attribute names such as [ "points", 0, "x" ], use a good JSON Pointer implementation, since most of them have convenience methods for translating these paths, and applying values to object graphs.

For example (Node.js):

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, "  "));

Produces:

{ points: [ { x: 7 } ], obj: { name: "Wilbur Finkle" } }
Community
  • 1
  • 1
flitbit
  • 168
  • 1
  • 2
  • 9