Copy object key in another key of same object - javascript

I have list of array associated with object. The requirement is to make a new key that will have all key value in there.
var obj = {
all:[
obj.one,
obj.two
],
one:[
'a',
'b',
'c'
],
two:[
'd',
'e',
'f',
'g'
]
}
//Wanted result
var obj = {
all:[
'a',
'b',
'c'
'd',
'e',
'f',
'g'
],
one:[
'a',
'b',
'c'
],
two:[
'd',
'e',
'f',
'g'
]
}

function accumulateKeyValues(obj){
var accumulated = [];
for(var key in obj)
accumulated = accumulated.concat(obj[key]);
obj["all"] = accumulated;
}
EXAMPLE:
function accumulateKeyValues(obj){
var accumulated = [];
for(var key in obj)
accumulated = accumulated.concat(obj[key]);
obj["all"] = accumulated;
}
var obj = {
one:[
'a',
'b',
'c'
],
two:[
'd',
'e',
'f',
'g'
]
};
accumulateKeyValues(obj);
console.log(obj);

You could get all keys of the object and take all values for a new array.
var obj = { one:['a', 'b', 'c'], two: ['d', 'e', 'f', 'g'] };
obj.all = Object.keys(obj).reduce(function (r, k) {
return r.concat(obj[k]);
}, []);
console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related

Can't find the longest occurancy in a set of duplicates

The target is to detect superstring in a set of arrays. In this case it should be 'bal' but I get 'lbal'.
const arr = [
['g', 'l', 'o', 'b', 'a', 'l'],
['b','a','l','l']
]
const res = argv.reduce((acc, val) => acc.filter(elem => val.includes(elem)))
This function just gives ALL duplicates(items that are presented in any array) when I need only the most long duplicate sequence. Any suggestions?
you can create an object that counts how many times an element is present in the array of array
like this
const arr = [
['a', 'b', 'm'],
['g', 'o', 'a', 'b'],
['w', 'o', 'u', 'k', 'a', 'b']
]
const countTimes = data => data.flat().reduce((res, v) => {
return {
...res,
[v]: (res[v] || 0 ) + 1
}
}, {})
const duplicates = data => Object.entries(countTimes(data))
.filter(([v, n]) => n > 1)
.map(([v, n]) => v)
console.log(countTimes(arr))
console.log(duplicates(arr) )
const original = [
['a', 'b', 'm'],
['g', 'o', 'a', 'b'],
['w', 'o', 'u', 'k', 'a', 'b']
]
// The easiest is to split up between unique results and duplicates
let uniqueValues = []
let duplicates = []
// Now we're going to loop every array
original.forEach((arr) => {
// Loop every value inside the array
arr.forEach((value) => {
// Check if we had this value already
if (!uniqueValues.includes(value)) {
uniqueValues.push(value)
} else {
duplicates.push(value)
}
})
})
console.log('Duplicates: ', duplicates)
// If you want remove the duplicates from the duplicates, use set
let uniqueDuplicates = [...new Set(duplicates)]
console.log('Unique duplicates: ', uniqueDuplicates)
If you don't have to know in which array duplicates are, you can use Array.prototype.flat() to get rid of nested arrays, then check simple array for duplicates.
const arr = [
['a', 'b', 'm'],
['g', 'o', 'a', 'b'],
['w', 'o', 'u', 'k', 'a', 'b']
]
const arr2 = arr.flat() // ['a', 'b', 'm', 'g', 'o', 'a', 'b', 'w', 'o', 'u', 'k', 'a', 'b']
const hasDuplicates = new Set(arr2).size !== arr2.length
you can create a tally, to find how many each element globally is duplicated by doing a nested forEach
function findDuplicates(data) {
const map = {};
data.forEach((row) => {
row.forEach((item) => {
if (!map[item]) {
map[item] = 1;
} else [map[item]++];
});
});
return map;
}
You can create a simple counter object (if you know python, this is similar to collections.Counter):
class Counter extends Map {
update(values) {
for (let val of values)
this.set(val, 1 + (this.get(val) ?? 0))
}
}
ARR = [
['a', 'b', 'm'],
['g', 'o', 'a', 'b'],
['w', 'o', 'u', 'k', 'a', 'b']
]
const tally = new Counter()
for (let subArray of ARR)
tally.update(new Set(subArray))
for (let [element, count] of tally)
if (count === ARR.length)
console.log(element)
count === yourArrayOfArrays.length selects elements that appear in all arrays, you can replace it with count > 1 to find any duplicates.

How to convert an array into an object of specific format?

Although it is a common problem but I couldn't find any lead to get the desired result. So here is the problem. I have the following array:
[
[ 'a' ]
[ 'a', 'b' ]
[ 'a', 'c' ]
[ 'a', 'c', 'd' ]
[ 'a', 'c', 'd', 'e' ]
]
And what I want as an end result is an object like this:
{
a: {
b: {},
c: { d: { e: {} } }
}
}
I don't understand which approach would be better to get this result and how to achieve it.
You need a double reduce, one for the outer array and one for the keys and the nesting objects.
var data = [['a'], ['a', 'b'], ['a', 'c'], ['a', 'c', 'd'], ['a', 'c', 'd', 'e']],
result = data.reduce((r, keys) => {
keys.reduce((o, k) => o[k] = o[k] || {}, r);
return r;
}, {});
console.log(result);

Group matching element in array

Given a sorted array, I'd like to create a new, 2D array containing arrays of matching elements. Similar to the behavior of python's itertools.groupby
Example:
input = ['a','a','a','a','d','e','e','f','h','h','h','i','l','m','n','r','s','s','t','u','v','y','y']
output = [ ['a','a','a','a'], ['d'], ['e','e'], ['f'], ['h','h','h'], ['i'], ['l'], ['m'], ['n'], ['r'], ['s','s'], ['t'], ['u'], ['v'], ['y','y']]
You could check the predecessor and add a new array before pushing to last item.
var input = ['a', 'a', 'a', 'a', 'd', 'e', 'e', 'f', 'h', 'h', 'h', 'i', 'l', 'm', 'n', 'r', 's', 's', 't', 'u', 'v', 'y', 'y'],
output = input.reduce(function (r, a, i, aa) {
if (aa[i - 1] !== a) {
r.push([]);
}
r[r.length - 1].push(a);
return r;
}, []);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For non sorted items, you could use a closure over a hash table.
var input = ['a', 'a', 'a', 'a', 'y', 'h', 'h', 'i', 'l', 'e', 'e', 'f', 'h', 'm', 'n', 'r', 's', 'y', 'd', 's', 't', 'u', 'v'],
output = input.reduce(function (hash) {
return function (r, a) {
if (!hash[a]) {
hash[a] = [];
r.push(hash[a]);
}
hash[a].push(a);
return r;
};
}(Object.create(null)), []);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Array.prototype.join() with parameter "", String.prototype.match() with RegExp /([a-z]+)(?=\1)\1|[^\1]/g to match one or more "a" through "z" followed by captured characters, or not captured group, .map(), .split()
var input = ['a', 'a', 'a', 'a'
, 'd', 'e', 'e', 'f'
, 'h', 'h', 'h', 'i'
, 'l', 'm', 'n', 'r'
, 's', 's', 't', 'u'
, 'v', 'y', 'y'];
var res = input.join("").match(/([a-z]+)(?=\1)\1|[^\1]/g).map(c => c.split(""));
console.log(res);
Note: This will work even if the array is not sorted:
var input = ['a','b','c','d','a','d','e','e','f','h','h','h','i','l','m','n','r','s','s','t','u','v','y','y'];
function group(arr) {
var hash = {};
return arr.reduce(function(res, e) {
if(hash[e] === undefined) // if we haven't hashed the index for this value
hash[e] = res.push([e]) - 1; // then hash the index which is the index of the newly created array that is initialized with e
else // if we have hashed it
res[hash[e]].push(e); // then push e to the array at that hashed index
return res;
}, []);
}
console.log(group(input));

Convert multiple arrays to single JSON

I need help converting several arrays:
x = ['a', 'b', 'c']
y = ['d', 'e', 'f']
z = ['d', 'g', 'h']
Into a single JSON:
{
a: { b: { c: 'done' }},
d: { e: { f: 'done' },
g: { h: 'done' }}
}
Is this possible using recursion? I can't seem to get it working properly so I was wondering if there is already an easy way to do this in JS.
var struct = {};
var paths = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['d', 'g', 'h']
];
paths.forEach(function (path) {
var ref;
ref = struct;
path.forEach(function (elem, index) {
if (!ref[elem]) {
ref[elem] = index === path.length - 1 ? "done": {};
}
ref = ref[elem];
});
});
console.log(JSON.stringify(struct, null, "\t"));
Output:
{
"a": {
"b": {
"c": "done"
}
},
"d": {
"e": {
"f": "done"
},
"g": {
"h": "done"
}
}
}
Note: this script will fail if your input is like:
var paths = [
['a', 'b', 'c'],
['a', 'b', 'c', 'd' ]
];
It decided c should be "done" but then there is another level. maybe this wont ever happen, if it does, figure out what you want the result to be.
A version with Array#forEach() and Array#reduce()
var x = ['a', 'b', 'c'],
y = ['d', 'e', 'f'],
z = ['d', 'g', 'h'],
object = function (array) {
var o = {};
array.forEach(function (a) {
var last = a.pop();
a.reduce(function (r, b) {
r[b] = r[b] || {};
return r[b];
}, o)[last] = 'done';
});
return o;
}([x, y, z]);
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

Convert flat array [k1,v1,k2,v2] to object {k1:v1,k2:v2} in JavaScript?

Is there a simple way in javascript to take a flat array and convert into an object with the even-indexed members of the array as properties and odd-indexed members as corresponding values (analgous to ruby's Hash[*array])?
For example, if I have this:
[ 'a', 'b', 'c', 'd', 'e', 'f' ]
Then I want this:
{ 'a': 'b', 'c': 'd', 'e': 'f' }
The best I've come up with so far seems more verbose than it has to be:
var arr = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
var obj = {};
for (var i = 0, len = arr.length; i < len; i += 2) {
obj[arr[i]] = arr[i + 1];
}
// obj => { 'a': 'b', 'c': 'd', 'e': 'f' }
Is there a better, less verbose, or more elegant way to do this? (Or I have just been programming in ruby too much lately?)
I'm looking for an answer in vanilla javascript, but would also be interested if there is a better way to do this if using undercore.js or jQuery. Performance is not really a concern.
Pretty sure this will work and is shorter:
var arr = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
var obj = {};
while (arr.length) {
obj[arr.shift()] = arr.shift();
}
See shift().
var arr = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
var obj = arr.reduce( function( ret, value, i, values ) {
if( i % 2 === 0 ) ret[ value ] = values[ i + 1 ];
return ret;
}, { } );
If you need it multiple times you can also add a method to the Array.prototype:
Array.prototype.to_object = function () {
var obj = {};
for(var i = 0; i < this.length; i += 2) {
obj[this[i]] = this[i + 1];
}
return obj
};
var a = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
a.to_object(); // => { 'a': 'b', 'c': 'd', 'e': 'f' }
You could first chunk your array into groups of two:
[['a', 'b'], ['c', 'd'], ['e', 'f']]
so that is is in a valid format to be used by Object.fromEntries(), which will build your object for you:
const chunk = (arr, size) => arr.length ? [arr.slice(0, size), ...chunk(arr.slice(size), size)] : [];
const arr = ['a', 'b', 'c', 'd', 'e', 'f'];
const res = Object.fromEntries(chunk(arr, 2));
console.log(res); // {a: "b", c: "d", e: "f"}
With underscore.js and lodash, you don't need to implement the chunk() method yourself, and can instead use _.chunk(), a method built into both libraries. The full lodash equivalent of the above would be:
// lodash
> _.fromPairs(_.chunk(arr, 2));
> {a: "b", c: "d", e: "f"}
Using _.fromPairs provides better browser support, so if using lodash, it is preferred over Object.fromEntries()
Similarly, we can use _.object() if you're using underscore.js to build the object:
// underscore.js
> _.object(_.chunk(arr, 2));
> {a: "b", c: "d", e: "f"}

Categories

Resources