I have a simple question. I have two arrays A and B, I want to return array of object with mixing the two arrays.
For example:
let a = [ 1, 2 ]
let b = [ 3, 4 ]
Expected result:
const C = [
{
a: 1,
b: 3
},
{
a: 2,
b: 4
}
]
How can I do this?
I tried to forloop A then B and assign everytime but it didn't work.
You can use array map method on one of the array and use index to retrieve the element from the second array
let a = [1, 2]
let b = [3, 4];
let c = a.map((item, index) => {
return {
a: item,
b: b[index]
}
});
console.log(c)
Something like this should work:
let a = [1, 2];
let b = [3, 4];
// From #brk. This transforms each element of a to an object containing a's value and b's value
let c = a.map((item, index) => {
a: item,
b: b[index]
});
// Another way. Iterates through each element
for (let i = 0; i < a.length; i++) {
c[i].a = a[i];
c[i].b = b[i];
}
// Yet another. A combination of the first two.
for (let [index, item] of Object.entries(a)) {
c[index] = {
a: item,
b: b[index]
};
}
There's certainly a more elegant solution, but it evades me at the moment
i am having 2 objects and the code is
Object 1
var existingValue=self.get('objHubDto').ftd.gridDataDTO.gridDTOMap[name].gridDataMap;
Object 2
var newValue = self.get('childDataList')
I need to merge/add/append this two in newvalue value eventhough it having duplicate key values
Following is a very basic example to merge by keeping both values, however, you can create new object rather mutating existing one.
let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = { a: 9, d: 8, e: 7 };
Object.entries(obj2).forEach(([key, val]) => {
obj1[key] = obj1[key] ? [obj1[key], val] : val;
});
console.log(obj1);
For the following:
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
function extend(obj1, obj2) {
for (i in obj2) {
if (!(i in obj1)) {
obj1[i] = obj2[i];
}
}
return obj1;
}
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
Why does obj2[i] assign the value and not the key? Shouldn't it assign the letter, as obj1[i] does?
Your code copies properties from obj2 to obj1 if the property to be copied doesn't yet exist in obj1.
for ... in iterates over keys, so i in your example will have value b and then c.
When i has value b, your if clause will not execute because the property b is already in obj1. When i has value c, if clause will execute, so the line in it will look like this
obj1['c'] = obj2['c']; // Assign value of obj2['c'] to property 'c' in obj1
which is essentially
obj['c'] = 3; // Assign value 3 to property 'c' in obj1
Who can explain how this for in loop works and why it's assign keys of object to array
var o = {
a: 1,
b: 2,
c: 3,
d: 4
};
var a = [],
i = 0;
for (a[i++] in o);
console.log(a);
Using a side effect when enumerating the object, and using an empty statement, each key is stored in the array a; first key in a[0], next in a[1] etc.
It is not necessary however since you could just use Object.keys(o)
var o = {
a: 1,
b: 2,
c: 3,
d: 4
};
var a = [],
i = 0;
for (a[i++] in o); // do nothing loop
console.log(a,Object.keys(o));
I have a object like that one:
Object {a: 1, b: 2, undefined: 1}
How can I quickly pull the largest value identifier (here: b) from it? I tried converting it to array and then sorting, but it didn't work out, since it got sorted alphabetically (and it seems like a overkill to juggle data back and forth just for getting one value out of three).
For example:
var obj = {a: 1, b: 2, undefined: 1};
Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });
In ES6:
var obj = {a: 1, b: 2, undefined: 1};
Object.keys(obj).reduce((a, b) => obj[a] > obj[b] ? a : b);
Using Underscore or Lo-Dash:
var maxKey = _.max(Object.keys(obj), function (o) { return obj[o]; });
With ES6 Arrow Functions:
var maxKey = _.max(Object.keys(obj), o => obj[o]);
jsFiddle demo
Here is a suggestion in case you have many equal values and not only one maximum:
const getMax = object => {
return Object.keys(object).filter(x => {
return object[x] == Math.max.apply(null,
Object.values(object));
});
};
This returns an array, with the keys for all of them with the maximum value, in case there are some that have equal values.
For example: if
const obj = {apples: 1, bananas: 1, pears: 1 }
//This will return ['apples', 'bananas', 'pears']
If on the other hand there is a maximum:
const obj = {apples: 1, bananas: 2, pears: 1 }; //This will return ['bananas']
---> To get the string out of the array: ['bananas'][0] //returns 'bananas'`
Supposing you've an Object like this:
var obj = {a: 1, b: 2, undefined: 1}
You can do this
var max = Math.max.apply(null,Object.keys(obj).map(function(x){ return obj[x] }));
console.log(Object.keys(obj).filter(function(x){ return obj[x] == max; })[0]);
{a: 1, b: 2, undefined: 1}
The best work around I've seen is this
const chars = {a: 1, b: 2, undefined: 1}
//set maximum value to 0 and maxKey to an empty string
let max = 0;
let maxKey = "";
for(let char in chars){
if(chars[char]> max){
max = chars[char];
maxKey= char
}
}
console.log(maxKey)
Very basic method. might be slow to process
var v = {a: 1, b: 2, undefined: 1};
function geth(o){
var vals = [];
for(var i in o){
vals.push(o[i]);
}
var max = Math.max.apply(null, vals);
for(var i in o){
if(o[i] == max){
return i;
}
}
}
console.log(geth(v));
Combination of some ideas from other answers. This will get all the keys with the highest value, but using the spread operator to get the maximum value and then filter array method:
const getMax = object => {
let max = Math.max(...Object.values(object))
return Object.keys(object).filter(key => object[key]==max)
}
let obj = {a: 12, b: 11, c: 12};
getMax(obj)
let data = {a:1,b:2,undefined:3}
let maxValue = Object.entries(data).sort((x,y)=>y[1]-x[1])[0]
note: this is a very expensive process and would block the event loop if used with objects of large sizes(>=1000000). with large array slice the entries and call the above method recurrently using setTimeout.
If you need to return an array from an object with the keys for all duplicate properties with the (same or equal) highest value, try this:
const getMax = Object.keys(object)
.filter(x => {
return object[x] == Math.max.apply(null,
Object.values(object));
})
var object = { orange: 3, blue: 3, green: 1}
console.log(getMax) // ['orange', 'blue']