Is there a way using lodash or another library to join an array of objects?
I'm looking for a readymade function not a for loop.
For example:
[{a: 1}, {a:3}, {a: 4}]
//Run a function by specifing the property a and setting "," as the delimeter
Get 1,3,4
Here is your lodash answer
var arr = [{a: 1}, {a:3}, {a: 4}];
var s = _.map(arr, 'a').join(',');
//s == '1,2,3,4'
You don't need lodash for this, you can just use map and join:
let collection = [{a: 1}, {a:3}, {a: 4}];
alert(collection.map(item => item.a).join(','));
Related
Given
a = [{"a": 1}, {"b": 0}, {"w", -4}]
I want to rearrange this into
a = [{"w", -4},{"b": 0}, {"a": 1}]
Sort by lowest to greatest
Trying to do this with a.sort()
You could use sort() and Object.values
a = [{"a": 1}, {"b": 0}, {"w": -4}]
a.sort((a,b)=>Object.values(a)[0]-Object.values(b)[0])
console.log(a)
Couln't you do it by passing by a map?
Maybe :
var map = new Map();
map.set("a", 1);
map.set("b", 0);
map.set("w", -4);
const sortedMapByValue= new Map([...map.entries()].sort((a, b) => a[1] - b[1]));
That normally gives you the result you want when iterating on sortedMapByValue
Edit: I haven't seen latest answer, pretty similar, better if you want to keep your array as is it
You could use sort with a custom comparator like so:
a.sort(( a, b, o=Object.values ) => o(a)[0] - o(b)[0] )
I have an object which could include a mix of properties e.g.
{a: 1}, {b: 4}, {c: 2}, {a: 3, b: 1}
it will be a key with a count value next to it.
I'd like to set a bunch of variables depending on which key names are in the object, for instance:
aOnly = {a: 1}, mixOfAB = {a: 3, b: 1}
I'm using this logic in a function which will eventually return a string value. What's the best way to operate on this object, I tried using a switch but it didn't work so well. I could use a large number of if/else statements but is there something more neater?
You could do something like this:
vars = {}
function setVariable(obj) {
keys = Object.keys(obj)
name = keys.length == 1 ? keys[0] + 'Only' : 'mixOf' + keys.join('').toUpperCase()
vars[name] = obj
}
setVariable({a: 1})
console.log(vars)
setVariable({b: 4})
console.log(vars)
setVariable({c: 2})
console.log(vars)
setVariable({a: 3, b: 1})
console.log(vars)
I have two arrays of objects, each object has .id property. I need to merge them so that in the return array, each object id is unique (object whos .id is already present in other object in that array, when trying to join the array is discarded). Preferably es6 way. Thanks!
var a = [{id: 2}, {id:3}]
var b = [{id: 4}, { id:3}]
mergeArrays(a,b)
// should return
////[{id: 2}, {id:3}, {id: 4}]`
Here's one approach: concatenate the two arrays into one and reduce them into an object by id which removes duplicates. Grabbing an array of values with Object.values() produces the desired result:
var a = [{id: 2}, {id:3}]
var b = [{id: 4}, {id:3}]
const result = Object.values(a.concat(b).reduce((a, e) => {
a[e.id] = e;
return a;
}, {}));
console.log(result);
Note that b's keys will overwrite any duplicate keys from a because of the order of the concatenation.
Is there any way to make _.orderBy alter the provided array? Using the example below it only seems to return a sorted result, leaving the provided array intact.
var arr = [{x: 1},{x: 2}];
console.log(_.orderBy(arr, 'x', 'desc')[0].x, arr[0].x);
https://jsfiddle.net/w5hoeurs/
As per Lodash documentation on orderBy:
Returns
(Array): Returns the new sorted array.
So, your code would be:
var arr = [{x: 1},{x: 2}];
arr = _.orderBy(arr,['x'],['desc']);
console.log(arr);
sry if this question is still there but i searched for it and i didn´t find a solution. Also i couldn´t get any hint out of the Developer.Mozilla website where Objects.assign etc. is described well.
The question:
How can i change the children of an object? With exactly the output i described in the following.
Sample-Code
var obj= {test: [{a: 2}, {b: 3}]};
var newChildren = [{a: 3}, {b: 5}, {c: 1}];
//I read the obj key value by the following:
var objKey = Object.entries(obj)[0][0]
//Here i want to have some code to get the following result
//My momentary regarding to #webdeb
var output = Object.assign({}, {objKey: newChildren})
actual output: // {objKey: [{a: 3}, {b: 5}, {c: 1}]}
wanted output: // {test: [{a: 3}, {b: 5}, {c: 1}]}
I have no other option to get the code in some other format so the "input" is needed in the way i described. And i need to set the objKey as a variable, because i have multiple keys in my code wich i have to set for multiple children. (Doing some filter stuff)
Thanks for your help
BR
Jonathan
First of all, don't use Object as variable Name..
Ok, lets say the Object is now obj
Without modification of the obj
var newObj = Object.assign({}, obj, {test: newChildren})
Or just, (modifies the obj)
obj.test = newChildren
Is it what you are looking for?
Following solution gave me the exact result i wanted:
obj[objKey] = newChildren;
console.log(obj) // {test: [{a: 3}, {b: 5}, {c: 1}]}
I took this solution regarding to the question:
How can I add a key/value pair to a JavaScript object?
The importance for me was to have the object-key as a variable. The described solution gives me the option to add a value tomore than just one static key.