Unflatten JS object and convert arrays - javascript

I have a function used to flatten objects like so:
let object = {
a: 1,
b: [
{ c: 2 },
{ c: 3 }
]
};
flatten(object)
// returns {
'a': 1,
'b.0.c': 2,
'b.1.c': 3
}
I need to unflatten objects, but also revert arrays to how they were. I have the following code:
unflatten(obj) {
let final = {};
for (let prop in obj) {
this.assign(final, prop.split('.'), obj[prop]);
}
return final;
}
assign(final, path, value) {
let lastKeyIndex = path.length-1;
for (var i = 0; i < lastKeyIndex; ++ i) {
let key = path[i];
if (!(key in final)) {
final[key] = {};
}
final = final[key];
}
final[path[lastKeyIndex]] = value;
}
which works for the most part, but it treats arrays like so:
{
a: 1,
b: { // Notice how b's value is now an object
"0": { c: 2 }, // Notice how these now have a key corresponding to their index
"1": { c: 3 }
}
}
Whereas I need b to be an array like before:
{
a: 1,
b: [
{ c: 2 },
{ c: 3 }
]
}
I'm at a loss for where to go from here. It needs to be able to deal with an arbitrary number of arrays like:
'a.b.0.c.0.d',
'a.b.0.c.1.d',
'a.b.1.c.0.d',
'a.b.1.c.1.d',
'a.b.1.c.2.d',
// etc
It needs to be vanilla JS, but es2015 is fine. It it assumed that any key that's a number is actually part of an array.
If anyone has any advice, it's appreciated!

When you find that key is not in final, you should check to see if the next key in the path is only digits (using a regular expression) and, if so, assign to an array instead of an object:
if (!(key in final)) {
final[key] = /^\d+$/.test(path[i + 1]) ? [] : {};
}
let object = {
a: 1,
b: [{
c: 2
},
{
c: 3
}
]
};
let flattened = {
'a': 1,
'b.0.c': 2,
'b.1.c': 3
}
function unflatten(obj) {
let final = {};
for (let prop in obj) {
assign(final, prop.split('.'), obj[prop]);
}
return final;
}
function assign (final, path, value) {
let lastKeyIndex = path.length - 1;
for (var i = 0; i < lastKeyIndex; ++i) {
let key = path[i];
if (!(key in final)) {
final[key] = /^\d+$/.test(path[i + 1]) ? [] : {};
}
final = final[key];
}
final[path[lastKeyIndex]] = value;
}
console.log(unflatten(flattened))
.as-console-wrapper { min-height: 100vh; }

You could iterate the keys and then split the string for single properties. For building a new object, you could check for number and take an array for these properties.
function setValue(object, path, value) {
var way = path.split('.'),
last = way.pop();
way.reduce(function (o, k, i, kk) {
return o[k] = o[k] || (isFinite(i + 1 in kk ? kk[i + 1] : last) ? [] : {});
}, object)[last] = value;
}
function unFlatten(object) {
var keys = Object.keys(object),
result = isFinite(keys[0][0]) ? [] : {};
keys.forEach(function (k) {
setValue(result, k, object[k]);
});
return result;
}
console.log(unFlatten({
'a': 1,
'b.0.c': 2,
'b.1.c': 3
}));
console.log(unFlatten({
'0': 1,
'1.0.c': 2,
'1.1.c': 3
}));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related

Iterate through an array with a single index that contains multiple objects

I have some data that looks like this
let arr = [
{
a:1,
b:2,
c:3
},
{
a:4,
b:5,
c:6
},
{
a:7,
b:8,
c:9
}
]
and I'd like to get it to reformat like this
{
a: [1,4,7],
b: [2,5,8],
c: [3,6,9]
}
Here is my solution:
let arr = [
{
a:1,
b:2,
c:3
},
{
a:4,
b:5,
c:6
},
{
a:7,
b:8,
c:9
}
]
// {
// a: [1,4,7],
// b: [2,5,8],
// c: [3,6,9]
// }
function practice (arr) {
console.log(typeof arr) // WHY IS THIS RETURNING AN OBJECT???
let keys = Object.keys(arr[0])
const resultObj = {}
for (let key of keys) {
resultObj[key] = []
}
arr.forEach((x,idx)=> {
for (let key in x) {
resultObj[key].push(x[key])
}
})
return resultObj
}
practice(arr)
I know that my solution is not the most efficient method. While I completed the exercise, I am having trouble understanding the concepts below:
At first glance, arr to me seems like an array with a single index
containing three objects. For example, arr[0] = {obj1},{obj2},{obj3}, but
I performed a typeof check on arr and it returned object.
When I console log arr at a specified index arr[1], it prints out {a:4,b:5,c:6} as if it is an array.
My question is what is happening here and what exactly is this type of data structure?
Please offer me a more clean and efficient code to this question and explain to me the concepts.
Try
function practice (arr) {
let resultObj = {};
arr.forEach((x) => {
for (let key in x) {
if (resultObj.hasOwnProperty(key)) {
resultObj[key].push(x[key]);
} else {
resultObj[key] = [x[key]];
}
}
});
return resultObj;
}
In order to check for an array, you should make use of Array.isArray() method. typeof will give you an object since Array is essentially a form of object in javascript created using the Object constructor.
To get a desired output, all you need to do is to loop over the array and store the values in an object
let arr = [
{
a:1,
b:2,
c:3
},
{
a:4,
b:5,
c:6
},
{
a:7,
b:8,
c:9
}
]
var res = {};
arr.forEach((obj) => {
Object.entries(obj).forEach(([key, val]) => {
if(res[key]) {
res[key].push(val);
} else {
res[key] = [val];
}
})
});
console.log(res);
You may use array reduce() method for this like:
Loop through all the keys of each object in the array using Object.keys(o) inside the reduce() method
Inside the loop, initialize the accumulator with the same key as we have inside the loop and the initial value of that key as empty array [].
Then using r[k].push(o[k]), we are adding the matching key values inside this array.
Then finally return the object r from the .reduce() method.
let arr = [{a:1,b:2,c:3},{a:4,b:5,c:6},{a:7,b:8,c:9}];
const res = arr.reduce((r, o) => {
Object.keys(o).forEach((k) => {
r[k] = r[k] || [];
r[k].push(o[k])
});
return r;
}, {})
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can try this-
let arr = [
{
a:1,
b:2,
c:3
},
{
a:4,
b:5,
c:6
},
{
a:7,
b:8,
c:9
}
];
let res = {};
Object.values(arr).forEach(value => {
Object.keys(value).forEach(key => {
if (typeof res[key] === 'undefined') {
res[key] = [];
}
res[key].push(value[key])
})
})
console.log(res);
With multiple forEach loops build an Object with the keys and value are merged for same key.
let arr = [
{
a: 1,
b: 2,
c: 3
},
{
a: 4,
b: 5,
c: 6
},
{
a: 7,
b: 8,
c: 9
}
];
const res = {};
arr.forEach(item => {
Object.keys(item).forEach(
key => (res[key] = key in res ? [...res[key], item[key]] : [item[key]])
);
});
console.log(res);

how to flatten a nested object using javascript

var obj = {
a: {
aa: {
aaa: {
aaaa: "a"
}
}
},
b: {
bb: {
bbb: "b"
}
}
}
flatten(obj)
//=>[["a","b"],["aa","bb"],["aaa","bbb"],["aaaa"]]
This is interesting question,my friend says BFS or DFS can be able to solve the problem,but I can't
you can use recursion and keep a level counter, on each step of recursion add keys to the result array. For this you have to check if an array already exists at that level then concatenate to that array.
var obj = {
a: {
aa: {
aaa: {
aaaa: "a"
}
}
},
b: {
bb: {
bbb: "b"
}
}
}
var result = [];
function flatten(obj, level){
var keys = Object.keys(obj);
result[level] = result[level] !== undefined ? result[level].concat(keys) : keys;
keys.forEach(x => typeof obj[x] === 'object' && flatten(obj[x], level + 1));
}
flatten(obj, 0);
console.log(result);

Compare 2 objects and remove repeating keys between

I am experimenting on objects, and what I am trying to achieve is to remove keys found in object1 if those keys exist in object2.
Here is the example:
var original = {
a: 1,
b: 2,
c: 3,
e: {
tester: 0,
combination: {
0: 1
}
},
0: {
test: "0",
2: "hello"
}
};
var badKeys = {
a: 1,
b: 2,
0: {
test: "0",
}
}
var expectedResult = {
c: 3,
e: {
tester: 0,
combination: {
0: 1
}
},
0: {
2: "hello"
}
}
I've tried using underscore difference function, but it doesn't work for objects, also not sure if this is the right function.
Can you help me to get the var expectedResult right?
You could use an iterative and recursive approach for geeting the wanted properties in a new object.
function deleteKeys(good, bad, result) {
Object.keys(good).forEach(function (key) {
if (bad[key] && typeof bad[key] === 'object') {
result[key] = {};
deleteKeys(good[key], bad[key], result[key]);
return;
}
if (!(key in bad) || good[key] !== bad[key]) {
result[key] = good[key];
}
});
}
var original = { a: 1, b: 2, c: 3, e: { tester: 0, combination: { 0: 1 } }, 0: { test: "0", 2: "hello", another: { a: { B: 2, C: { a: 3 } }, b: 2 } } },
badKeys = { a: 1, b: 2, 0: { test: "0", random: 2, another: { a: 1 } } },
result = {};
deleteKeys(original, badKeys, result);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This would be the algorithm:
function removeDifferences (original, removeKeys) {
// Get keys of to be deleted properties.
var keys = Object.keys(removeKeys);
// Iterate all properties on removeKeys.
for (key of keys) {
// Check if property exists on original.
if (typeof original[key] !== undefined) {
// If the property is an object, call same function to remove properties.
if (typeof removeKeys[key] === 'object') {
removeDifferences(original[key], removeKeys[key]);
} else {
delete original[key];
}
}
}
return original;
}
Applied to your case:
/* Your data. */
var original = {
a: 1,
b: 2,
c: 3,
e: {
tester: 0,
combination: {
0: 1
}
},
0: {
test: "0",
2: "hello"
}
};
var badKeys = {
a: 1,
b: 2,
0: {
test: "0",
}
};
var expectedResult = {
c: 3,
e: {
tester: 0,
combination: {
0: 1
}
},
0: {
2: "hello"
}
};
/* Function */
function removeDifferences(original, removeKeys) {
// Get keys of to be deleted properties.
var keys = Object.keys(removeKeys);
// Iterate all properties on removeKeys.
for (key of keys) {
// Check if property exists on original.
if (typeof original[key] !== undefined) {
// If the property is an object, call same function to remove properties.
if (typeof removeKeys[key] === 'object') {
removeDifferences(original[key], removeKeys[key]);
} else {
delete original[key];
}
}
}
return original;
}
/* Application */
var output = removeDifferences(original, badKeys);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can create recursive function that will return new object using for...in loop.
var original = {"0":{"2":"hello","test":"0"},"a":1,"b":2,"c":3,"e":{"tester":0,"combination":{"0":1}}}
var badKeys = {"0":{"test":"0"},"a":1,"b":2}
function remove(o1, o2) {
var result = {}
for (var i in o1) {
if (!o2[i]) result[i] = o1[i]
else if (o2[i]) {
if (typeof o1[i] == 'object' && typeof o2[i] == 'object') {
result[i] = Object.assign(result[i] || {}, remove(o1[i], o2[i]))
} else if (o1[i] != o2[i]) result[i] = o1[i]
}
}
return result
}
console.log(remove(original, badKeys))
Truly a job for some recursion and a bit of functional programming using a pure function. (Tested with Node v7.7.1)
"DoForAllNestedObjects" for applying some function "whattodo" on "every leaf on the dictionary tree" when there is an corresponding "leaf" in baddict.
let DoForAllNestedValues = (dict, baddict, whattodo) => {
for (let key in dict) {
if (typeof (dict[key]) === 'object' && typeof (baddict[key]) === 'object')
DoForAllNestedValues(dict[key], baddict[key], whattodo);
else
if (baddict[key])
whattodo(dict, key);
}
}
DoForAllNestedValues(original, badKeys, (obj, val) => delete obj[val]);
console.log(original);

Sort array of objects based on multiple properties

I'm trying to sort array of objects based on arbitrary property inside the object.
I wrote the following code which works perfectly.
var customSort = function(data, sortBy, order) {
if (!(data && sortBy)) {
throw new Error('Specify the data source and atleast one property to sort it by');
}
if (!Array.isArray(data)) {
throw new Error('Specify the data source as an array');
}
order = order || 'asc';
function performSort(order, sortBy) {
return data.sort(function(a, b) {
var A = parse(a, sortBy);
var B = parse(b, sortBy);
if (A < B) {
return order === 'asc' ? -1 : 1;
} else if (A > B) {
return order === 'asc' ? 1 : -1;
} else {
return 0;
}
});
}
function parse(data, sortBy) {
var sortParams = sortBy.split('.');
var latestValue = data;
for (var i = 0; i < sortParams.length; i++) {
latestValue = latestValue[sortParams[i]];
}
if (!latestValue) {
throw new Error('Check the \'sortBy\' parameter. Unreachable parameter in \'sortBy\'');
}
if (typeof latestValue === 'string') {
return latestValue.toLowerCase();
} else {
return latestValue;
}
}
return performSort(order, sortBy);
};
var lonelyData = [{
a: {
b: 2
}
}, {
a: {
b: 1
}
}, {
a: {
b: 9
}
}, {
a: {
b: 7
}
}, {
a: {
b: 4
}
}, {
a: {
b: 2
}
}, {
a: {
b: 1
}
}];
console.log(customSort(lonelyData, 'a.b'));
Fiddle here: JSFiddle
Now, I'm trying to use recursion to have sort by multiple properties in following manner: First, sort by first property and then within that same property, sort by second property, and so on. For keeping things simple, I'm assuming same order for all the properties, i.e. either all increasing or all decreasing.
I wrote this code:
var customSort = function(data, sortBy, order) {
if (!(data && sortBy)) {
throw new Error('Specify the data source and atleast one property to sort it by');
}
if (!Array.isArray(data)) {
throw new Error('Specify the data source as an array');
}
if (!Array.isArray(sortBy)) {
sortBy = [sortBy];
}
order = order || 'asc';
function performSort(order, sortBy) {
return data.sort(function(a, b) {
function nestedSort() {
var highestOrder = sortBy[0];
var A = parse(a, highestOrder);
var B = parse(b, highestOrder);
if (A < B) {
return order === 'asc' ? -1 : 1;
} else if (A > B) {
return order === 'asc' ? 1 : -1;
} else {
sortBy.shift();
nestedSort();
}
}
return nestedSort();
});
}
function parse(data, sortBy) {
var sortParams = sortBy.split('.');
var latestValue = data;
for (var i = 0; i < sortParams.length; i++) {
latestValue = latestValue[sortParams[i]];
}
if (!latestValue) {
throw new Error('Check the \'sortBy\' parameter. Unreachable property passed in \'sortBy\'');
}
if (typeof latestValue === 'string') {
return latestValue.toLowerCase();
} else {
return latestValue;
}
}
return performSort(order, sortBy);
};
var lonelyData = [{
a: {
b: 2,
c: 'Z'
}
}, {
a: {
b: 2,
c: 'A'
}
}, {
a: {
b: 9,
c: 'Q'
}
}, {
a: {
b: 7,
c: 'L'
}
}, {
a: {
b: 7,
c: 'E'
}
}, {
a: {
b: 1,
c: 'A'
}
}, {
a: {
b: 1,
c: 'B'
}
}];
console.log(customSort(lonelyData, ['a.b', 'a.c']));
Fiddle here: JSFiddle
Unfortunately, I couldn't make it work. What am I missing here?
sortBy.shift();
nestedSort();
is your problem. shift does mutate the array, and drop the first item forever. The next time the comparison function is called to compare a with b, it will be ignored, and soon the array will be empty and your comparsion breaks completely.
Instead, use a simple loop:
return data.sort(function(a, b) {
for (var i=0; i<sortBy.length; i++)
var currentOrder = sortBy[i];
var A = parse(a, currentOrder);
var B = parse(b, currentOrder);
if (A < B) {
return order === 'asc' ? -1 : 1;
} else if (A > B) {
return order === 'asc' ? 1 : -1;
}
}
return 0;
});

Is there anything like 'lies in' operator in js, as we have '$in' in mongoose/mongo

i want to implement a function like this in js
function(arrayOfObjects, arrayOfValues, property) {
/*here i want to return all the objects of 'arrayOfObjects'
which satisfies the following condition
(index is the iterative index of array 'arrayOfObjects')
arrayOfObjects[index][property] is equivalent to any of
the values that lies in arrayOfValues */
};
example :
arrayOfObjects = [{ a: 1, b: 2 }, { a: 3, b:4 }, { a: 1, b :3 }];
arrayOfValues = [ 2, 3 ];
function(arrayOfObjects, arrayOfValues, 'b')
should return [{ a: 1, b: 2 }, { a: 1, b :3 }]
arrayOfObjects.filter(function (elem) {
return elem.hasOwnProperty(property)
&& -1 !== arrayOfValues.indexOf(elem[property]);
});
In case you need IE8 support: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Compatibility
You can use the Array.prototype.filter functionality:
var a1 = [{ a: 1, b: 2 }, { a: 3, b:4 }, { a: 1, b :3 }];
var a2 = [2, 3];
var filtered = a1.filter(function(item) {
return a2.indexOf(item.b) != -1;
});
No, this is too complex problem to have some build-in operator or function in JS.
You must use some cycle to walk through the elements.
function(arrayOfObjects, arrayOfValues, property) {
var result = [];
for (var i = 0; i < arrayOfObjects; i++) {
for (var j = 0; j < arrayOfValues; j++) {
if (arrayOfObjects[i][property] === arrayOfValues[j]) {
result.push(arrayOfObjects[i]);
continue; //object already added, go to next one
}
}
}
return result;
};
function fun1(arrayOfObjects, arrayOfValues, property) {
var result = new Array();
for (var obj in arrayOfObjects){
if (arrayOfObjects[obj].hasOwnProperty(property) &&
arrayOfObjects[obj][property] == arrayOfValues[property]){
result.push(arrayOfObjects[obj]);
}
}
return result;
}
var arrayOfObjects = [{ a: 1, b: 2 }, { a: 3, b:4 }, { a: 1, b :3 }];
var arrayOfValues = new Array();
arrayOfValues['a'] = 2;
arrayOfValues['b'] = 3;
console.log(fun1(arrayOfObjects , arrayOfValues , 'b'));
Your arrayOfValues should be an 'associative array' or key-value-pair object which use key to match the property. This might more suit your logic.

Categories