Related
let obj = { T: 1, h: 1, i: 3, s: 3, t: 1, r: 1, n: 1, g: 1 };
In the given object we have 2 keys(i & s both have same value 3) contains same value. I need to keep one key/value pair and another wants to remove.
How we can achieve this?
Make a copy and leave one field out?
let obj2 = { T: obj.T, h: obj.h, i: obj.i, t: obj.t, r: obj.r, n: obj.n, g: obj.g };
;-)
It's not super clear if you want to omit all keys with duplicate values or just some keys. In case you want the latter, here's a reusable function for that:
function copyWithout(source, ...keysToOmit) {
return Object.entries(source).reduce(
(accumulator, [key, value]) => {
return keysToOmit.includes(key)
? accumulator
: Object.assign(accumulator, { [key]: value });
},
Object.create(null) // or just {}
);
}
And use it like so:
copyWithout(obj, 'i');
Can be used to omit multiple keys as well:
copyWithout(obj, 'i', 't', 'r');
You can loop over object keys and add values in some temp array. Verify from that temp array and if value already exist in it then delete that key from object. Try like below.
let obj = { T: 1, h: 1, i: 3, s: 3, t: 1, r: 1, n: 1, g: 1 };
let val = [];
// loop over keys and verify that if value is repeating then delete that key from object.
Object.keys(obj).forEach(k => {
if (val.includes(obj[k])) {
delete obj[k];
} else {
val.push(obj[k])
}
});
console.log(obj);
Alternatively if you wish to keep your object as it is and want output in another object then try like below.
let obj = { T: 1, h: 1, i: 3, s: 3, t: 1, r: 1, n: 1, g: 1 };
let val = [];
let newObj = {};
// loop over keys and verify that if value is repeating then delete that key from object.
Object.keys(obj).forEach(k => {
if (!Object.values(newObj).includes(obj[k])) {
newObj[k] = obj[k];
}
});
console.log('newObj', newObj);
console.log('obj', obj);
You can use the following and swap key values twice,
let obj = { T: 1, h: 1, i: 3, s: 3, t: 1, r: 1, n: 1, g: 1 };
function swapKV(ob) {
return Object.entries(ob).reduce((p,[key,value]) => ({...p,[value]: key}),{})
}
swapKV(swapKV(obj))
You could save all unique values in set. This is the most optimal solution since it takes O(n) in time.
let obj = { T: 1, h: 1, i: 3, s: 3, t: 1, r: 1, n: 1, g: 1 };
function removeDuplicateValues(obj) {
const uniqueValues = new Set();
const uniquePairs = {};
Object.keys(obj).forEach(key => {
const curValue = obj[key];
if (!uniqueValues.has(curValue)) {
uniqueValues.add(curValue);
uniquePairs[key] = curValue;
}
});
return uniquePairs;
}
console.log(removeDuplicateValues(obj)) // {T: 1, i: 3}
const object = { a: 1, b: 2, c: 3, d: 2, e: 3 };
const removeUniqueValuesFromObject = (object) => {
const map = new Map();
// loop through all the attributes on an object
for (const [key, value] of Object.entries(object)) {
// check if the value already exists in map, if yes delete the attribute
if (map.has(value)) {
delete object[`${key}`];
continue;
}
// if value not found, add it to the map
map.set(value, key);
}
// return the updated object
return object;
};
const result = removeUniqueValuesFromObject(object);
console.log(result);
I have an object with keys and values. Each value is an array that describes the position of key in the string.
const input = {
' ': [5],
d: [10],
e: [1],
H: [0],
l: [2,3,9],
o: [4,7],
r: [8],
w: [6],
};
const buildString = (m) => {
}
I heed to return the string Hello world . My solution is shown below:
const buildString = (m) => {
let res = [];
for(let key in m) {
m[key].map(el => {
res[el] = key;
})
}
return res.join('');
}
But I suppose that it can be solved using reduce method. Could someone help me with implementation please?
Thanks in advance.
There you go
const input = {
' ': [5],
d: [10],
e: [1],
H: [0],
l: [2,3,9],
o: [4,7],
r: [8],
w: [6],
};
const words = Object.entries(input)
.reduce( (acc, [character, positions]) => {
// | ^ Object.entries gives you an array of [key, value] arrays
// ^ acc[umulator] is the array (second parameter of reduce)
positions.forEach(v => acc[v] = character);
// ^ put [character] # the given [positions] within acc
return acc;
}, [])
.join("");
// ^ join the result to make it a a string
console.log(words);
You need to take double nested loops, one for iterating the entries and the other to get the index.
For collecting the characters, you need an array and return the joined array.
const
input = { ' ': [5], d: [10], e: [1], H: [0], l: [2, 3, 9], o: [4, 7], r: [8], w: [6] },
buildString = (m) => {
const letters = [];
for (let [character, indices] of Object.entries(m)) {
for (const index of indices) {
letters[index] = character;
}
}
return letters.join('');
};
console.log(buildString(input));
Probably could be done better, but here's my attempt.
It uses Object.entries to "convert" input into an array of key/value array pairs.
Then it loops through each entry and it's positions and puts them in the correct place in the accumulator array.
Finally, it uses the array method join to convert the accumulator array to a string.
const input = {
' ': [5],
d: [10],
e: [1],
H: [0],
l: [2, 3, 9],
o: [4, 7],
r: [8],
w: [6],
};
const buildString = (m) => {
return Object.entries(m).reduce((acc, [key, positions], ind) => {
positions.forEach(val => acc[val] = key);
return acc;
}, []).join('');
}
console.log(buildString(input))
Reduce doesnt make things simpler everytime, but anyway:
const input = {
' ': [5],
d: [10],
e: [1],
H: [0],
l: [2,3,9],
o: [4,7],
r: [8],
w: [6],
};
const result = Object.entries(input).reduce((word, entry) => {
const [letter, indices] = entry;
for (const index of indices) {
word[index] = letter;
}
return word;
}, []).join('');
console.log(result);
I have an array of objects, e.g.:
[
{ a: 3, b: 2, c: 5, d: 6, e: 8 },
{ a: 1, b: 5, c: 3, d: 1, e: 2 }
]
Now I want to transform this to an array that contains only the values of specific properties, but without the objects themselves. E.g., if I am interested in a, b, c, and d, the result should look like this:
[ 3, 2, 5, 6, 1, 5, 3, 1 ]
My current approach looks like this:
const result = _.flatten(data.map(item => [ item.a, item.b, item.c, item.d ]));
Is there a better (i.e., more efficient, and maybe even more readable) way to get the result?
What you have seems plenty readable to me, and is likely efficient enough. But you can make it more efficient by avoiding all those temporary arrays and not looping twice:
const result = [];
data.forEach(item => result.push(item.a, item.b, item.c, item.d));
Example:
const data = [
{ a: 3, b: 2, c: 5, d: 6, e: 8 },
{ a: 1, b: 5, c: 3, d: 1, e: 2 }
];
const result = [];
data.forEach(item => result.push(item.a, item.b, item.c, item.d));
console.log(result);
Some engines do push really efficiently, others not so much. If efficiency were a critical requirement, you'd want to experiment comparing that with this on your target environments:
const result = [];
let index = 0;
let n, l, item;
for (n = 0, l = data.length; n < l; ++n) {
item = data[n];
result[index++] = item.a;
result[index++] = item.b;
result[index++] = item.c;
result[index++] = item.d;
}
Three things to note there:
Pushing directly onto the array instead of using push.
Using a simple for loop instead of forEach. The absolute overhead of using forEach is so small as to be virtually non-existant from a human perspective, but the relative overhead of it in a tight loop is quite large.
Declaring i, l, and item outside the for loop. If we declared them within the for, they'd be recreated on each loop iteration, adding overhead. (ES2015's semantics for let declarations are powerful and useful, but in this particular case, we don't want the overhead.)
Example:
const data = [
{ a: 3, b: 2, c: 5, d: 6, e: 8 },
{ a: 1, b: 5, c: 3, d: 1, e: 2 }
];
const result = [];
let index = 0;
let n, l, item;
for (n = 0, l = data.length; n < l; ++n) {
item = data[n];
result[index++] = item.a;
result[index++] = item.b;
result[index++] = item.c;
result[index++] = item.d;
}
console.log(result);
You could reduce with mapped keys.
var array = [{ a: 3, b: 2, c: 5, d: 6, e: 8 }, { a: 1, b: 5, c: 3, d: 1, e: 2 }],
keys = ['a', 'b', 'c', 'd'],
result = array.reduce((r, a) => r.concat(keys.map(k => a[k])), []);
console.log(result);
You can Array.prototype.map() the desired keys from the objects in the array and merge all them with Function.prototype.apply() and Array.prototype.concat():
let data = [{ a: 3, b: 2, c: 5, d: 6, e: 8 }, { a: 1, b: 5, c: 3, d: 1, e: 2 }],
result = [].concat.apply([], data.map(i => [i.a, i.b, i.c, i.d]));
console.log(result);
In ES6, and making no claims for efficiency, you could write
const input = [
{ a: 3, b: 2, c: 5, d: 6, e: 8 },
{ a: 1, b: 5, c: 3, d: 1, e: 2 }
];
console.log([].concat(...input.map(({a, b, c, d}) => [a, b, c, d])));
If you're really interested in efficiency, nothing would beat
var result = [];
for (let i = 0; i < input.length; i++) {
const o = input[i];
result.push(o.a, o.b, o.c, o.d);
}
although as another answer points out, it would be worth benchmarking push against result[cnt++] = val.
You can use 2 nested forEach loops to push targeted values of each element of your input array. I propose here an function receiving as parameters initial array and properties targeted as an other array :
var inflate = (data, options) => {
var results = [];
data.forEach(x => options.forEach(y => results.push(x[y])));
return results;
}
console.log(inflate([{ a: 3, b: 2, c: 5, d: 6, e: 8 }, { a: 1, b: 5, c: 3, d: 1, e: 2 }] ,["a","b","c","d"]));
I have an object like:
Object {v: 1, b: 1, n: 1, m: 1, c: 2, d: 3}
I am trying to collect keys that has same values and generate an array (so in this case [v, b, n, m].
So if I know 1, how can I get v,b,n,m in an array?
You can use Object.keys() to get key values of the object, then filter values based on your condition using filter()
var obj = {
v: 1,
b: 1,
n: 1,
m: 1,
c: 2,
d: 3
},
val = 1;
var res = Object.keys(obj).filter(function(v) {
return obj[v] == val;
});
document.write('<pre>' + JSON.stringify(res, null, 4) + '</pre>');
Performance? Simple for...in loop.
var myObj = {v: 1, b: 1, n: 1, m: 1, c: 2, d: 3};
function collect (obj, value) {
var arr = [];
for (var key in obj)
if (obj.hasOwnProperty(key) && obj[key] === value)
arr.push(key);
return arr;
}
console.log(collect(myObj, 1));
Fancy? Object.keys, and Array.prototype.filter.
var myObj = {v: 1, b: 1, n: 1, m: 1, c: 2, d: 3};
function collect (obj, value) {
return Object.keys(obj).filter(key => obj[key] === value);
}
console.log(collect(myObj, 1));
using the underscore.js library you can do the following
var object = {v: 1, b: 1, n: 1, m: 1, c: 2, d: 3};
var output = _.map(object, function(value, key){
if(value == 1){
return key
}
}).filter(function(value){
return value;
});
console.log(output);
I have two object literals like so:
var firstObject =
{
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30
}
var secondObject =
{
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30
}
I want to get the intersection of the keys these two object literals have like so:
var intersectionKeys = ['x', 'y', 'z', 'a']
I can obviously do a loop and see if a key with the same name exists in the other object, but I am wondering if this would be a good case for some functional programming and map / filter / reduce usage? I myself have not done that much functional programming, but I have a feeling, that there could exist a clean and clever solution for this problem.
A solution without indexOf.
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).concat(Object.keys(o2)).sort().reduce(function (r, a, i, aa) {
if (i && aa[i - 1] === a) {
r.push(a);
}
return r;
}, []);
}
document.write('<pre>' + JSON.stringify(intersection(firstObject, secondObject), 0, 4) + '</pre>');
Second attempt with O(n).
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).filter({}.hasOwnProperty.bind(o2));
}
document.write('<pre>' + JSON.stringify(intersection(firstObject, secondObject), 0, 4) + '</pre>');
The given answers are nice and astonishing but there could be a problem in void's answer and that is:
"What if one of property values intentionally set to undefined."
Nina's answer is good (really fantastic) but as we are in era of fun JavaScript I think mine wont be too bad:
var a = { x: undefined, y: 1, z: 2, a: 10, b: 20, e: 30 }
var b = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 }
function intersect(o1, o2){
return Object.keys(o1).filter(k => Object.hasOwn(o2, k))
}
document.write('<pre>' + JSON.stringify(intersect(a, b)) + '</pre>');
Update
onalbi mentioned some performance issue in comments which is rational and therefore the code bellow seems to be a better way to handle the problem:
var a = { x: undefined, y: 1, z: 2, a: 10, b: 20, e: 30};
var b = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30};
function intersect(o1, o2) {
const [k1, k2] = [Object.keys(o1), Object.keys(o2)];
const [first, next] = k1.length > k2.length ? [k2, o1] : [k1, o2];
return first.filter(k => k in next);
}
document.write('<pre>' + JSON.stringify(intersect(a, b)) + '</pre>');
The procedure i will suggest is:
Get the array of keys using Object.keys() for one of the objects.
Find the intersection the array using .filter and checking if the second object contains a key matching the first array.
var firstObject = {
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30
}
var secondObject = {
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30
}
function getIntKeys(obj1, obj2){
var k1 = Object.keys(obj1);
return k1.filter(function(x){
return obj2[x] !== undefined;
});
}
alert(getIntKeys(firstObject, secondObject));
Recursive function
This is other solution, maybe help you. I used a recursive function to intercept two objects. The advantage of this solution is that you not need worry about attributes that are objects at same time.
In this case the function intercept attributes that exist in both objects and asign the value of 'objSource' like final value of attribute intercepeted.
{
function interceptObjects(objSource, objInterface) {
let newObj = {};
for (const key in objSource) {
if (objInterface.hasOwnProperty(key)) {
// in javascript an array is a object too.
if (objSource[key] instanceof Object && !Array.isArray(objSource[key]) && objInterface[key] instanceof Object && !Array.isArray(objInterface[key])) {
newObj[key] = {};
newObj[key] = interceptObjects(objSource[key], objInterface[key])
} else {
newObj[key] = objSource[key];
}
}
}
return newObj;
}
// FOR TESTING
let objSource = {
attr1: '',
attr2: 2,
attr3: [],
attr4: {
attr41: 'lol',
attr42: 12,
attr43: 15,
attr45: [1, 4],
},
attr5: [2, 3, 4],
};
let objInterface = {
attr1: null,
attr4: {
attr41: null,
attr42: 12,
attr45: [1],
},
attr5: [],
attr6: null,
};
console.log(this.interceptObjects(objSource, objInterface));
}
Here is a simple entry, very functional, handles any number of objects, and returns the values of the matching keys from the first object passed.
This behavior is similar to that of array_intersect_key() in PHP in case anyone is searching for that.
function intersectKeys(first, ...rest) {
const restKeys = rest.map(o => Object.keys(o));
return Object.fromEntries(Object.entries(first).filter(entry => restKeys.every(rk => rk.includes(entry[0]))));
}
Expanded here for better explanation and commenting
function intersectKeys(first, ...rest) {
// extract the keys of the other objects first so that won't be done again for each check
const restKeys = rest.map(o => Object.keys(o));
// In my version I am returning the first objects values under the intersect keys
return Object.fromEntries(
// extract [key, value] sets for each key and filter them, Object.fromEntries() reverses this back into an object of the remaining fields after the filter
Object.entries(first).filter(
// make sure each of the other object key sets includes the current key, or filter it out
entry => restKeys.every(
rk => rk.includes(entry[0])
)
)
);
// to get JUST the keys as OP requested the second line would simplify down to this
return Object.keys(first).filter(key => restKeys.every(rk => rk.includes(key)));
}
It's important to note that this solution only works on string keys, Symbol keys will be ignored and the final object will not contain any. Though a similar function could be written to compare Symbol intersect as well.
I know this is an old post, however, I want to share a solution I wrote today that I believe is efficient and clean.
function intersectingKeys(...objects) {
return objects
.map((object) => Object.keys(object))
.sort((a, b) => a.length - b.length)
.reduce((a, b) => a.filter((key) => b.includes(key)));
}
This function can take in n number of objects, and find the intersecting keys.
This is how it works.
Map the objects, creating an array of key arrays.
Sort the array by length, this puts the smallest key arrays first.
Finally, reduce our key arrays, by filtering each list of keys against the next list.
I think the clever part of this algorithm is the pre sorting of the key arrays. By starting with the smallest list of keys, we have less work to do comparing keys.
Here is the usuage:
var firstObject = {
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30,
};
var secondObject = {
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30,
};
intersectingKeys(firstObject, secondObject);
// [ 'x', 'y', 'z', 'a' ]