how to add parents name in an object (got parent count ) - javascript

I am trying to make a object in which it property have parent count and parent names. I am able to count the parents ..But I want to add the parent name in my code..
Here is my code
https://jsfiddle.net/ood2ezvz/11/
Code
function getParentCount(nodes) {
var parent = {}, o = {};
nodes.forEach(function (n) {
parent[n.node_from] = parent[n.node_from] || [];
n.children.forEach(function (a) {
parent[a.node_to] = parent[a.node_to] || [];
parent[a.node_to].push(n.node_from);
});
});
Object.keys(parent).forEach(function (k) { o[k] = parent[k].length; });
return o;
}
my output
{11: 0, 12: 1, 13: 1, 14: 1, 15: 2, 16: 1, 17: 1, 18: 1, 19: 1}
Expected out put
{
11:{count:0,parent:[]},
12:{count:1,parent:['11']},
13:{count:1,parent:['12']},
14:{count:1,parent:['13']},
15:{count:2,parent:['13','14']},
16:{count:1,parent:['15']},
17:{count:1,parent:['15']},
18:{count:1,parent:['15']},
19:{count:1,parent:['18']},
}

Try to replace :
Object.keys(parent).forEach(function (k) { o[k] = parent[k].length; });
By :
Object.keys(parent).forEach(function (k) {
o[k] = {};
o[k]['count'] = parent[k].length;
o[k]['parent'] = parent[k];
});

I'd suggest converting your structure to something more usable. Since it seems to be a directed graph, a natural representation will be the list of pairs [from, to]:
graph = []
node.forEach(n =>
n.children.forEach(c =>
graph.push([n.node_from, c.node_to])
)
)
Now you can easily find preceding and following nodes for every given node:
nodes_from = n => graph.filter(v => v[0] === n).map(v => v[1])
console.log(nodes_from(15)) // [ 16, 17, 18 ]
nodes_to = n => graph.filter(v => v[1] === n).map(v => v[0])
console.log(nodes_to(15)) // [ 13, 14 ]

Related

How to build map for large json with unknown structure

I have large json data with unknown depth and I need to build a map in the following format of result.
const json = {
1: {
11: {
111: [{ "111-0": "b" }, { "111-1": [{ "111-1-0": "vs" }] }],
112: "asasd",
...
},
12: [{ "12-0": "sd" }],
...
},
2: [{ "2-0": "sd" }],
....
};
const result = {
"1::11::111::A0::111-0": "b",
"1::11::111::A1::111-1::A0::111-1-0": "vs",
"1::11::112": "asasd",
"1::12::A0::12-0": "sd",
"2::A0::2-0": "sd",
};
I think recursion is a good way to solve this but I am not able to implement recursion properly.
This is my current progress. Which gives incorrect output.
const buildRecursion = (json, r, idx = 0, prev = "") => {
Object.keys(json).forEach((key) => {
prev += key + "::";
if (Array.isArray(json[key])) {
for (let [i, v] of json[key].entries()) {
buildRecursion(v, r, i, prev);
}
} else if (typeof json[key] === "object") {
buildRecursion(json[key], r, "", prev);
} else {
if (idx === "") {
r[prev + "::" + key + "::"] = json[key];
} else {
r[prev + "::" + key + "::" + "::A" + idx] = json[key];
}
}
});
};
I'm glad to say, you're on the right track. All I did was clean up your variables
(especialy your handling of prev) and it works fine.
Other notes,
use '' instead of "" for strings
consider using template strings (backticks) for concatenating strings instead of + when doing so is cleaner (more often than not).
I renamed the vars json -> input, r -> output, prev -> key for clarity.
let input = {
1: {
11: {
111: [{"111-0": "b"}, {"111-1": [{"111-1-0": "vs"}]}],
112: "asasd",
},
12: [{"12-0": "sd"}],
},
2: [{"2-0": "sd"}],
};
let buildRecursion = (input, output = {}, key = []) => {
if (Array.isArray(input))
input.forEach((v, i) =>
buildRecursion(v, output, [...key, `A${i}`]));
else if (typeof input === 'object')
Object.entries(input).forEach(([k, v]) =>
buildRecursion(v, output, [...key, k]));
else
output[key.join('::')] = input;
return output;
};
let result = buildRecursion(input);
console.log(result);
// {
// "1::11::111::A0::111-0": "b",
// "1::11::111::A1::111-1::A0::111-1-0": "vs",
// "1::11::112": "asasd",
// "1::12::A0::12-0": "sd",
// "2::A0::2-0": "sd",
// }
You could use reduce method and forEach if the value is array. Then you can use Object.assign to assign recursive call result to the accumulator of reduce which is the result object.
const json = {"1":{"11":{"111":[{"111-0":"b"},{"111-1":[{"111-1-0":"vs"}]}],"112":"asasd"},"12":[{"12-0":"sd"}]},"2":[{"2-0":"sd"}]}
function f(data, prev = '') {
return Object.entries(data).reduce((r, [k, v]) => {
const key = prev + (prev ? '::' : '') + k
if (typeof v === 'object') {
if (Array.isArray(v)) {
v.forEach((o, i) => Object.assign(r, f(o, key + `::A${i}`)))
} else {
Object.assign(r, f(v, key))
}
} else {
r[key] = v
}
return r
}, {})
}
const result = f(json);
console.log(result)
(Updated to handle a missing requirement for the 'A' in the front of array indices.)
You can build this atop a function which associates deeper paths with the value for all leaf nodes. I use variants of this pathEntries function in other answers, but the idea is simply to traverse the object, collecting leaf nodes and the paths that lead to it.
const pathEntries = (obj) =>
Object (obj) === obj
? Object .entries (obj) .flatMap (
([k, x]) => pathEntries (x) .map (([p, v]) => [[Array.isArray(obj) ? Number(k) : k, ... p], v])
)
: [[[], obj]]
const compress = (obj) =>
Object .fromEntries (
pathEntries (obj)
.map (([p, v]) => [p.map(n => Number(n) === n ? 'A' + n : n) .join ('::') , v])
)
const json = {1: {11: {111: [{ "111-0": "b" }, { "111-1": [{ "111-1-0": "vs" }] }], 112: "asasd", }, 12: [{ "12-0": "sd" }], }, 2: [{ "2-0": "sd" }]}
console .log (compress (json))
The result of pathEntries on your data would look like this:
[
[["1", "11", "111", 0, "111-0"], "b"],
[["1", "11", "111", 1, "111-1", 0, "111-1-0"], "vs"],
[["1", "11", "112"], "asasd"],
[["1", "12", 0, "12-0"], "sd"],
[["2", 0, "2-0"], "sd"]
]
Then compress maps those path arrays into strings, adding the 'A' to the front of the numeric paths used for arrays and calls Object .fromEntries on those results. If you're working in an environment without Object .fromEntries it's easy enough to shim.
While compress is specific to this requirement, pathEntries is fairly generic.

Rearrange dynamic Object to form a structured Object

I have an Object like this:
const val = {"abc":{"1":1, "2":6,"3":5},"def":{"1":3, "2":4,"3":8},"xyz":{"1":5, "2":6,"3":7}}
I want to transform the object data like below:
[{"abc":1,"def":3,"xyz":5},{"abc":6,"def":4,"xyz":6}, ...]
All the values are dynamic, any number of inner object may be there
I have tried like this:
const val = {"abc":{"1":1, "2":6,"3":5},"def":{"1":3, "2":4,"3":8},"xyz":{"1":5, "2":6,"3":7}}
let dataObj = {};
let secondArr = [];
let dataArr =[]
Object.entries(val).map(firstObj=>{
Object.entries(firstObj[1]).forEach(secondObj => {
dataObj={[firstObj[0]]:secondObj[1]};
secondArr.push(dataObj);
})
dataArr.push(secondArr)
})
console.log(dataArr)
Can anyone tell me a solution for this?
Thanks in advance
You could iterate the entries of the objects and take the inner keys as indices of the array with new objects with outer key and value.
var data = { abc: { 1: 1, 2: 6, 3: 5 }, def: { 1: 3, 2: 4, 3: 8 }, xyz: { 1: 5, 2: 6, 3: 7 } },
result = Object
.entries(data)
.reduce((r, [k, o]) => {
Object.entries(o).forEach(([i, v]) =>
Object.assign(r[i - 1] = r[i - 1] || {}, { [k]: v }));
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to display nested key of object from left to right?

I have an object
var tree = {
17:{
1:{
3:{},
4:{}
},
2:{
5:{},
6:{}
}
}
};
How to display the keys in this order 17, 1, 2, 3,4, 5,6 ?
i have tried this function:
var arr = [] ;
var arrObject = [] ;
function printValues(obj) {
for (var key in obj) {
arr.push(key);
if (typeof obj[key] === "object") {
arrObject.push(obj[key]);
printValues(obj[key]);
}
}
}
printValues(tree);
the result is 17,1,3,42,5,6 .
and i need 17, 1, 2, 3,4, 5,6
The following uses a recursive function to extract all the nested keys associated with their depth in the object. It then sorts the results by depth.
Note that the approach may change the order of the keys at a given depth. To demonstrate this, I change the 3 in your example to 99. The returned results include "4", "99", not "99", "4". However, as pointed out in the comment by #deceze, the order of properties in an object is undefined anyway, so changing the order within a given object depth shouldn't (hopefully) be a problem. (If it is a problem then you need a different approach, e.g. using an array instead of an object.)
Note also that the code below returns an array of strings. This would be especially appropriate if some of your keys are explicitly strings, e.g. using letters. If you really want the results to be numerical, just add a + to the map command, i.e. ... .map(elmt => +elmt[1]).
const getKeysOrderedByDepth = obj => {
let arr = [], level = 0;
const getKeys = obj => {
level += 1;
for (let key in obj) {
arr.push([level, key]);
const val = obj[key];
getKeys(obj[key]);
}
level -= 1;
};
getKeys(obj);
return arr.sort((a, b) => a[0] - b[0]).map(elmt => elmt[1]);
};
let tree = {
17: {
1: {
99: {},
4: {}
},
2: {
5: {},
6: {}
}
}
};
console.log(getKeysOrderedByDepth(tree));
This is works for me(changed):
const tree = {
first:{
1:{
3:{
7: 7,
8: 8,
},
4:{
9: 9,
10: 10,
}
},
2:{
5:{
20: {
30: 30,
},
21: {
31: 31,
}
},
6:{
22: 22,
}
},
},
};
const keys = [];
/**
* Handle merged values
* #param values
*/
function handleData(values) {
let subValues = {};
for (let key in values) {
keys.push(key);
Object.assign(subValues, handle(values[key]));
}
// If have values, handle them
if (Object.keys(subValues).length) handleData(subValues);
}
/**
* Handle one value
* #param value
* #returns {{}}
*/
function handle(value) {
if (Object.keys(value).length) {
return Object.assign({}, value);
}
}
// Handle our data
handleData(tree);
console.log(keys); // ['first',1,2,3,4,5,6,7,8,9,10,20,21,22,30,31]

Full path of a json object

I'm trying to flatten an object where the keys will be the full path to the leaf node. I can recursively identify which are the leaf nodes but stuck trying to construct the whole path.
Sample Input:
{
one: 1,
two: {
three: 3
},
four: {
five: 5,
six: {
seven: 7
},
eight: 8
},
nine: 9
}
Output:
{
one: 1,
'two.three': 3,
'four.five': 5,
'four.six.seven': 7,
'four.eight': 8,
nine: 9
}
You could use a recursive approch and collect the keys of the object. This proposal looks for arrays as well.
function getFlatObject(object) {
function iter(o, p) {
if (o && typeof o === 'object') {
Object.keys(o).forEach(function (k) {
iter(o[k], p.concat(k));
});
return;
}
path[p.join('.')] = o;
}
var path = {};
iter(object, []);
return path;
}
var obj = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 },
path = getFlatObject(obj);
console.log(path);
Partial solution :
Give the input as a full path to the function and it gives you the respective output
var obj = {
one: 1,
two: {
three: 3
},
four: {
five: 5,
six: {
seven: 7
},
eight: 8
},
nine: 9
};
function deepFind(obj, path) {
var paths = path.split('.')
, current = obj
, i;
for (i = 0; i < paths.length; ++i) {
if (current[paths[i]] == undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
}
console.log(deepFind(obj, 'four.six.seven'))
Using newest JS features like Object spread and Object.entries it should be pretty easy:
function flatObj(obj, path = []) {
let output = {};
Object.entries(obj).forEach(([ key, value ]) => {
const nextPath = [ ...path, key ];
if (typeof value !== 'object') {
output[nextPath.join('.')] = value;
return;
}
output = {
...output,
...flatObj(value, nextPath)
};
});
}
Please note that this code is probably not the most optimal one as it copies the object each time we want to merge it. Treat it more as a gist of what would it look like, rather than a complete and final solution.
var obj = {
one: 1,
two: {
three: 3
},
four: {
five: 5,
six: {
seven: 7
},
eight: 8
},
nine: 9
};
function flatten(obj) {
var flatObj = {}
function makeFlat(obj, path) {
var keys = Object.keys(obj);
if (keys.length) {
keys.forEach(function (key) {
makeFlat(obj[key], (path ? path + "." : path) + key);
})
} else {
flatObj[path] = obj;
}
}
makeFlat(obj, "");
return flatObj;
}
console.log(flatten(obj));
A non fancy approach, internally uses recursion.
var x = { one:1,two:{three:3},four:{five: 5,six:{seven:7},eight:8},nine:9};
var res = {};
var constructResultCurry = function(src){ return constructResult(res,src); }
function constructResult(target, src) {
if(!src) return;
target[src.key] = src.val;
}
function buildPath(key, obj, overAllKey) {
overAllKey += (overAllKey ? "." : "") + key;
if(typeof obj[key] != "object") return { key : overAllKey, val : obj[key] };
Object.keys(obj[key]).forEach(function(keyInner) {
constructResultCurry(buildPath(keyInner, obj[key], overAllKey));
});
}
Object.keys(x).forEach(function(k){
constructResultCurry(buildPath(k, x, ""));
});
console.log(res);
You might simply do as follows;
var obj = {one: 1, two: {three: 3}, four: {five: 5, six: {seven: 7}, eight: 8}, nine: 9},
flatObj = (o,p="") => { return Object.keys(o)
.map(k => o[k] === null ||
typeof o[k] !== "object" ? {[p + (p ? ".":"") + k]:o[k]}
: flatObj(o[k],p + (p ? ".":"") + k))
.reduce((p,c) => Object.assign(p,c));
};
console.log(flatObj(obj));
I find a tiny JavaScript utility to access properties using path. It is called object-path and is an opensource project on GitHub.
To get attribute from an object:
objectPath.get(obj, "a.b");
to set attribute:
objectPath.set(obj, "a.b", value);
to remove an attribute:
objectPath.del(obj, "a.b");
So easy!!
You can achieve it by using this function:
const obj = {
one: 1,
two: {
three: 3
},
four: {
five: 5,
six: {
seven: 7
},
eight: 8
},
nine: 9
}
const flatObject = (obj, keyPrefix = null) =>
Object.entries(obj).reduce((acc, [key, val]) => {
const nextKey = keyPrefix ? `${keyPrefix}.${key}` : key
if (typeof val !== "object") {
return {
...acc,
[nextKey]: val
};
} else {
return {
...acc,
...flatObject(val, nextKey)
};
}
}, {});
console.log(flatObject(obj))
Here is an interative solution using object-scan.
object-scan is a data processing tool, so the main advantage here is that it would be easy to do further processing or processing while extracting the desired information
// const objectScan = require('object-scan');
const myData = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 };
const flatten = (data) => {
const entries = objectScan(['**'], {
reverse: false,
rtn: 'entry',
joined: true,
filterFn: ({ isLeaf }) => isLeaf
})(data);
return Object.fromEntries(entries);
};
console.log(flatten(myData));
// => { one: 1, 'two.three': 3, 'four.five': 5, 'four.six.seven': 7, 'four.eight': 8, nine: 9 }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.8.0"></script>
Disclaimer: I'm the author of object-scan
Try this
let x;
try{
x = JSON.parse(prompt("Input your JSON"))
}
catch(e) {
alert("not a valid json input")
}
var res = {};
var constructResultCurry = function(src){ return constructResult(res,src); }
function constructResult(target, src) {
if(!src) return;
target[src.key] = src.val;
}
function buildPath(key, obj, overAllKey) {
overAllKey += (overAllKey ? "." : "") + key;
if(typeof obj[key] != "object") return { key : overAllKey, val : obj[key] };
Object.keys(obj[key]).forEach(function(keyInner) {
constructResultCurry(buildPath(keyInner, obj[key], overAllKey));
});
}
Object.keys(x).forEach(function(k){
constructResultCurry(buildPath(k, x, ""));
});
console.log("**************ALL FIELDS****************")
console.log(res);
console.log("******************************************")
let conf = confirm("do you need a specific field from JSON");
if ( conf )
{
let field = prompt("Input field name")
let results = Object.fromEntries(
Object.entries(res).filter(([key]) => (key.toLowerCase()).includes((field.toLowerCase()))))
prompt("Copy to clipboard: Ctrl+C, Enter", JSON.stringify(results));
console.log(results)
}
else {
prompt("Copy to clipboard: Ctrl+C, Enter", JSON.stringify(res));
}
https://jsfiddle.net/amars404/2n9fprz8/57/

Comparing Arrays of Objects in JavaScript

I want to compare 2 arrays of objects in JavaScript code. The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see if anyone had a more elegant solution. Any thoughts?
As serialization doesn't work generally (only when the order of properties matches: JSON.stringify({a:1,b:2}) !== JSON.stringify({b:2,a:1})) you have to check the count of properties and compare each property as well:
const objectsEqual = (o1, o2) =>
Object.keys(o1).length === Object.keys(o2).length
&& Object.keys(o1).every(p => o1[p] === o2[p]);
const obj1 = { name: 'John', age: 33};
const obj2 = { age: 33, name: 'John' };
const obj3 = { name: 'John', age: 45 };
console.log(objectsEqual(obj1, obj2)); // true
console.log(objectsEqual(obj1, obj3)); // false
If you need a deep comparison, you can call the function recursively:
const obj1 = { name: 'John', age: 33, info: { married: true, hobbies: ['sport', 'art'] } };
const obj2 = { age: 33, name: 'John', info: { hobbies: ['sport', 'art'], married: true } };
const obj3 = { name: 'John', age: 33 };
const objectsEqual = (o1, o2) =>
typeof o1 === 'object' && Object.keys(o1).length > 0
? Object.keys(o1).length === Object.keys(o2).length
&& Object.keys(o1).every(p => objectsEqual(o1[p], o2[p]))
: o1 === o2;
console.log(objectsEqual(obj1, obj2)); // true
console.log(objectsEqual(obj1, obj3)); // false
Then it's easy to use this function to compare objects in arrays:
const arr1 = [obj1, obj1];
const arr2 = [obj1, obj2];
const arr3 = [obj1, obj3];
const arraysEqual = (a1, a2) =>
a1.length === a2.length && a1.every((o, idx) => objectsEqual(o, a2[idx]));
console.log(arraysEqual(arr1, arr2)); // true
console.log(arraysEqual(arr1, arr3)); // false
EDIT: You cannot overload operators in current, common browser-based implementations of JavaScript interpreters.
To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply serialize the two arrays to JSON and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to each of the objects within the arrays as well to see which ones were different.
Another option is to use a library which has some nice facilities for comparing objects - I use and recommend MochiKit.
EDIT: The answer kamens gave deserves consideration as well, since a single function to compare two given objects would be much smaller than any library to do what I suggest (although my suggestion would certainly work well enough).
Here is a naïve implemenation that may do just enough for you - be aware that there are potential problems with this implementation:
function objectsAreSame(x, y) {
var objectsAreSame = true;
for(var propertyName in x) {
if(x[propertyName] !== y[propertyName]) {
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
The assumption is that both objects have the same exact list of properties.
Oh, and it is probably obvious that, for better or worse, I belong to the only-one-return-point camp. :)
Honestly, with 8 objects max and 8 properties max per object, your best bet is to just traverse each object and make the comparisons directly. It'll be fast and it'll be easy.
If you're going to be using these types of comparisons often, then I agree with Jason about JSON serialization...but otherwise there's no need to slow down your app with a new library or JSON serialization code.
I know this is an old question and the answers provided work fine ... but this is a bit shorter and doesn't require any additional libraries ( i.e. JSON ):
function arraysAreEqual(ary1,ary2){
return (ary1.join('') == ary2.join(''));
}
I have worked a bit on a simple algorithm to compare contents of two objects and return an intelligible list of difference. Thought I would share. It borrows some ideas for jQuery, namely the map function implementation and the object and array type checking.
It returns a list of "diff objects", which are arrays with the diff info. It's very simple.
Here it is:
// compare contents of two objects and return a list of differences
// returns an array where each element is also an array in the form:
// [accessor, diffType, leftValue, rightValue ]
//
// diffType is one of the following:
// value: when primitive values at that index are different
// undefined: when values in that index exist in one object but don't in
// another; one of the values is always undefined
// null: when a value in that index is null or undefined; values are
// expressed as boolean values, indicated wheter they were nulls
// type: when values in that index are of different types; values are
// expressed as types
// length: when arrays in that index are of different length; values are
// the lengths of the arrays
//
function DiffObjects(o1, o2) {
// choose a map() impl.
// you may use $.map from jQuery if you wish
var map = Array.prototype.map?
function(a) { return Array.prototype.map.apply(a, Array.prototype.slice.call(arguments, 1)); } :
function(a, f) {
var ret = new Array(a.length), value;
for ( var i = 0, length = a.length; i < length; i++ )
ret[i] = f(a[i], i);
return ret.concat();
};
// shorthand for push impl.
var push = Array.prototype.push;
// check for null/undefined values
if ((o1 == null) || (o2 == null)) {
if (o1 != o2)
return [["", "null", o1!=null, o2!=null]];
return undefined; // both null
}
// compare types
if ((o1.constructor != o2.constructor) ||
(typeof o1 != typeof o2)) {
return [["", "type", Object.prototype.toString.call(o1), Object.prototype.toString.call(o2) ]]; // different type
}
// compare arrays
if (Object.prototype.toString.call(o1) == "[object Array]") {
if (o1.length != o2.length) {
return [["", "length", o1.length, o2.length]]; // different length
}
var diff =[];
for (var i=0; i<o1.length; i++) {
// per element nested diff
var innerDiff = DiffObjects(o1[i], o2[i]);
if (innerDiff) { // o1[i] != o2[i]
// merge diff array into parent's while including parent object name ([i])
push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + i + "]" + o[0]; return o; }));
}
}
// if any differences were found, return them
if (diff.length)
return diff;
// return nothing if arrays equal
return undefined;
}
// compare object trees
if (Object.prototype.toString.call(o1) == "[object Object]") {
var diff =[];
// check all props in o1
for (var prop in o1) {
// the double check in o1 is because in V8 objects remember keys set to undefined
if ((typeof o2[prop] == "undefined") && (typeof o1[prop] != "undefined")) {
// prop exists in o1 but not in o2
diff.push(["[" + prop + "]", "undefined", o1[prop], undefined]); // prop exists in o1 but not in o2
}
else {
// per element nested diff
var innerDiff = DiffObjects(o1[prop], o2[prop]);
if (innerDiff) { // o1[prop] != o2[prop]
// merge diff array into parent's while including parent object name ([prop])
push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + prop + "]" + o[0]; return o; }));
}
}
}
for (var prop in o2) {
// the double check in o2 is because in V8 objects remember keys set to undefined
if ((typeof o1[prop] == "undefined") && (typeof o2[prop] != "undefined")) {
// prop exists in o2 but not in o1
diff.push(["[" + prop + "]", "undefined", undefined, o2[prop]]); // prop exists in o2 but not in o1
}
}
// if any differences were found, return them
if (diff.length)
return diff;
// return nothing if objects equal
return undefined;
}
// if same type and not null or objects or arrays
// perform primitive value comparison
if (o1 != o2)
return [["", "value", o1, o2]];
// return nothing if values are equal
return undefined;
}
I tried JSON.stringify() and worked for me.
let array1 = [1,2,{value:'alpha'}] , array2 = [{value:'alpha'},'music',3,4];
JSON.stringify(array1) // "[1,2,{"value":"alpha"}]"
JSON.stringify(array2) // "[{"value":"alpha"},"music",3,4]"
JSON.stringify(array1) === JSON.stringify(array2); // false
There is a optimized code for case when function needs to equals to empty arrays (and returning false in that case)
const objectsEqual = (o1, o2) => {
if (o2 === null && o1 !== null) return false;
return o1 !== null && typeof o1 === 'object' && Object.keys(o1).length > 0 ?
Object.keys(o1).length === Object.keys(o2).length &&
Object.keys(o1).every(p => objectsEqual(o1[p], o2[p]))
: (o1 !== null && Array.isArray(o1) && Array.isArray(o2) && !o1.length &&
!o2.length) ? true : o1 === o2;
}
Here is my attempt, using Node's assert module + npm package object-hash.
I suppose that you would like to check if two arrays contain the same objects, even if those objects are ordered differently between the two arrays.
var assert = require('assert');
var hash = require('object-hash');
var obj1 = {a: 1, b: 2, c: 333},
obj2 = {b: 2, a: 1, c: 444},
obj3 = {b: "AAA", c: 555},
obj4 = {c: 555, b: "AAA"};
var array1 = [obj1, obj2, obj3, obj4];
var array2 = [obj3, obj2, obj4, obj1]; // [obj3, obj3, obj2, obj1] should work as well
// calling assert.deepEquals(array1, array2) at this point FAILS (throws an AssertionError)
// even if array1 and array2 contain the same objects in different order,
// because array1[0].c !== array2[0].c
// sort objects in arrays by their hashes, so that if the arrays are identical,
// their objects can be compared in the same order, one by one
var array1 = sortArrayOnHash(array1);
var array2 = sortArrayOnHash(array2);
// then, this should output "PASS"
try {
assert.deepEqual(array1, array2);
console.log("PASS");
} catch (e) {
console.log("FAIL");
console.log(e);
}
// You could define as well something like Array.prototype.sortOnHash()...
function sortArrayOnHash(array) {
return array.sort(function(a, b) {
return hash(a) > hash(b);
});
}
My practice implementation with sorting, tested and working.
const obj1 = { name: 'John', age: 33};
const obj2 = { age: 33, name: 'John' };
const obj3 = { name: 'John', age: 45 };
const equalObjs = ( obj1, obj2 ) => {
let keyExist = false;
for ( const [key, value] of Object.entries(obj1) ) {
// Search each key in reference object and attach a callback function to
// compare the two object keys
if( Object.keys(obj2).some( ( e ) => e == key ) ) {
keyExist = true;
}
}
return keyExist;
}
console.info( equalObjs( obj1, obj2 ) );
Compare your arrays
// Sort Arrays
var arr1 = arr1.sort(( a, b ) => {
var fa = Object.keys(a);
var fb = Object.keys(b);
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
});
var arr2 = arr2.sort(( a, b ) => {
var fa = Object.keys(a);
var fb = Object.keys(b);
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
});
const equalArrays = ( arr1, arr2 ) => {
// If the arrays are different length we an eliminate immediately
if( arr1.length !== arr2.length ) {
return false;
} else if ( arr1.every(( obj, index ) => equalObjs( obj, arr2[index] ) ) ) {
return true;
} else {
return false;
}
}
console.info( equalArrays( arr1, arr2 ) );
I am sharing my compare function implementation as it might be helpful for others:
/*
null AND null // true
undefined AND undefined // true
null AND undefined // false
[] AND [] // true
[1, 2, 'test'] AND ['test', 2, 1] // true
[1, 2, 'test'] AND ['test', 2, 3] // false
[undefined, 2, 'test'] AND ['test', 2, 1] // false
[undefined, 2, 'test'] AND ['test', 2, undefined] // true
[[1, 2], 'test'] AND ['test', [2, 1]] // true
[1, 'test'] AND ['test', [2, 1]] // false
[[2, 1], 'test'] AND ['test', [2, 1]] // true
[[2, 1], 'test'] AND ['test', [2, 3]] // false
[[[3, 4], 2], 'test'] AND ['test', [2, [3, 4]]] // true
[[[3, 4], 2], 'test'] AND ['test', [2, [5, 4]]] // false
[{x: 1, y: 2}, 'test'] AND ['test', {x: 1, y: 2}] // true
1 AND 1 // true
{test: 1} AND ['test', 2, 1] // false
{test: 1} AND {test: 1} // true
{test: 1} AND {test: 2} // false
{test: [1, 2]} AND {test: [1, 2]} // true
{test: [1, 2]} AND {test: [1]} // false
{test: [1, 2], x: 1} AND {test: [1, 2], x: 2} // false
{test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 5}]} // true
{test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 6}]} // false
*/
function is_equal(x, y) {
const
arr1 = x,
arr2 = y,
is_objects_equal = function (obj_x, obj_y) {
if (!(
typeof obj_x === 'object' &&
Object.keys(obj_x).length > 0
))
return obj_x === obj_y;
return Object.keys(obj_x).length === Object.keys(obj_y).length &&
Object.keys(obj_x).every(p => is_objects_equal(obj_x[p], obj_y[p]));
}
;
if (!( Array.isArray(arr1) && Array.isArray(arr2) ))
return (
arr1 && typeof arr1 === 'object' &&
arr2 && typeof arr2 === 'object'
)
? is_objects_equal(arr1, arr2)
: arr1 === arr2;
if (arr1.length !== arr2.length)
return false;
for (const idx_1 of arr1.keys())
for (const idx_2 of arr2.keys())
if (
(
Array.isArray(arr1[idx_1]) &&
this.is_equal(arr1[idx_1], arr2[idx_2])
) ||
is_objects_equal(arr1[idx_1], arr2[idx_2])
)
{
arr2.splice(idx_2, 1);
break;
}
return !arr2.length;
}
Please try this one:
function used_to_compare_two_arrays(a, b)
{
// This block will make the array of indexed that array b contains a elements
var c = a.filter(function(value, index, obj) {
return b.indexOf(value) > -1;
});
// This is used for making comparison that both have same length if no condition go wrong
if (c.length !== a.length) {
return 0;
} else{
return 1;
}
}
The objectsAreSame function mentioned in #JasonBunting's answer works fine for me. However, there's a little problem: If x[propertyName] and y[propertyName] are objects (typeof x[propertyName] == 'object'), you'll need to call the function recursively in order to compare them.
not sure about the performance ... will have to test on big objects .. however, this works great for me.. the advantage it has compared to the other solutions is, the objects/array do not have to be in the same order ....
it practically takes the first object in the first array, and scans the second array for every objects .. if it's a match, it will proceed to another
there is absolutely a way for optimization but it's working :)
thx to #ttulka I got inspired by his work ... just worked on it a little bit
const objectsEqual = (o1, o2) => {
let match = false
if(typeof o1 === 'object' && Object.keys(o1).length > 0) {
match = (Object.keys(o1).length === Object.keys(o2).length && Object.keys(o1).every(p => objectsEqual(o1[p], o2[p])))
}else {
match = (o1 === o2)
}
return match
}
const arraysEqual = (a1, a2) => {
let finalMatch = []
let itemFound = []
if(a1.length === a2.length) {
finalMatch = []
a1.forEach( i1 => {
itemFound = []
a2.forEach( i2 => {
itemFound.push(objectsEqual(i1, i2))
})
finalMatch.push(itemFound.some( i => i === true))
})
}
return finalMatch.every(i => i === true)
}
const ar1 = [
{ id: 1, name: "Johnny", data: { body: "Some text"}},
{ id: 2, name: "Jimmy"}
]
const ar2 = [
{name: "Jimmy", id: 2},
{name: "Johnny", data: { body: "Some text"}, id: 1}
]
console.log("Match:",arraysEqual(ar1, ar2))
jsfiddle: https://jsfiddle.net/x1pubs6q/
or just use lodash :))))
const _ = require('lodash')
const isArrayEqual = (x, y) => {
return _.isEmpty(_.xorWith(x, y, _.isEqual));
};
using _.some from lodash: https://lodash.com/docs/4.17.11#some
const array1AndArray2NotEqual =
_.some(array1, (a1, idx) => a1.key1 !== array2[idx].key1
|| a1.key2 !== array2[idx].key2
|| a1.key3 !== array2[idx].key3);
There`s my solution. It will compare arrays which also have objects and arrays. Elements can be stay in any positions.
Example:
const array1 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];
const array2 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];
const arraysCompare = (a1, a2) => {
if (a1.length !== a2.length) return false;
const objectIteration = (object) => {
const result = [];
const objectReduce = (obj) => {
for (let i in obj) {
if (typeof obj[i] !== 'object') {
result.push(`${i}${obj[i]}`);
} else {
objectReduce(obj[i]);
}
}
};
objectReduce(object);
return result;
};
const reduceArray1 = a1.map(item => {
if (typeof item !== 'object') return item;
return objectIteration(item).join('');
});
const reduceArray2 = a2.map(item => {
if (typeof item !== 'object') return item;
return objectIteration(item).join('');
});
const compare = reduceArray1.map(item => reduceArray2.includes(item));
return compare.reduce((acc, item) => acc + Number(item)) === a1.length;
};
console.log(arraysCompare(array1, array2));
This is work for me to compare two array of objects without taking into consideration the order of the items
const collection1 = [
{ id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },
{ id: "2", name: "item 2", parentId: "1" },
{ id: "3", name: "item 3", parentId: "1" },
]
const collection2 = [
{ id: "3", name: "item 3", parentId: "1" },
{ id: "2", name: "item 2", parentId: "1" },
{ id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },
]
const contains = (arr, obj) => {
let i = arr.length;
while (i--) {
if (JSON.stringify(arr[i]) === JSON.stringify(obj)) {
return true;
}
}
return false;
}
const isEqual = (obj1, obj2) => {
let n = 0
if (obj1.length !== obj2.length) {
return false;
}
for (let i = 0; i < obj1.length; i++) {
if (contains(obj2, obj1[i])) {
n++
}
}
return n === obj1.length
}
console.log(isEqual(collection1,collection2))
if you take into consideration the order of the items use built in function in lodash isEqual
comparing with json is pretty bad. try this package to compare nested arrays and get the difference.
https://www.npmjs.com/package/deep-object-diff
If you stringify them...
type AB = {
nome: string;
}
const a: AB[] = [{ nome: 'Célio' }];
const b: AB[] = [{ nome: 'Célio' }];
console.log(a === b); // false
console.log(JSON.stringify(a) === JSON.stringify(b)); // true

Categories

Resources