How to get object values from lodash? - javascript

say I have this most basic object
var x = {
a: 1,
b: 2,
c: 3,
d: 4
}
if I do this Object.values(x) this returns me an array of the values [1, 2, 3, 4]
how can I do this in lodash?
I know i can use get
_.get(x)
is just returning me undefined. I want the same thing as Object.values returns me but using lodash

Use _.values
var x = {a: 1,b: 2,c: 3,d: 4};
var result = _.values(x);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Just take _.values.
var x = { a: 1, b: 2, c: 3, d: 4 };
console.log(_.values(x));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

try with
var x = {
a: 1,
b: 2,
c: 3,
d: 4
}
const result = _.values(x);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
Reference:
https://lodash.com/docs/4.17.10#values

Related

How to convert an array containing arrays of objects to single array witht hose object using Ramda expressions?

I am having a difficult time, there is some bad mapping going on on my code.
I have an array containing array of objects like that :
[
[{a: 1, b: 2},{a: 1, b: 3} ],
[{a: 5, b: 2},{a: 2, b: 5}]
]
And I want to make like that :
[
{a: 1, b: 2},
{a: 1, b: 3},
{a: 5, b: 2},
{a: 2, b: 5}
]
In order to do that, I thought I found the magical solution, make things flat, using flatten function, it was not working ( this problem is just a piece of code in a lot of code ) and I was wondering why, i wasted some time to find that this the problem, it is not the behovior I am expecting, as you can see in the image, the first thing I have is an array containing an array having two objects, with flatten method, I was expecting an array of two objects, but I am getting what you see in the image :
The code I have ttried is this :
const expectedArray = R.flatten(myArrayOfArraysOfObjects);
Full example :
const singleTronconPoints = troncon => {
return troncon.geometri_linestring;
};
console.log('troncons : ');
console.log(troncons);
console.log('map troncons points');
console.log(map(singleTronconPoints, troncons));
console.log('flatten');
console.log(flatten(map(singleTronconPoints, troncons)));
and this is full result :
How can I solve that, is there another magical ( :P ) solution ( method ) to solve the problem ?
Any help would be much appreciated.
Array.prototype.reduce() can also be an option:
const arr =[
[{a: 1, b: 2},{a: 1, b: 3}],
[{a: 5, b: 2},{a: 2, b: 5}]
]
const expectedArray = arr.reduce((acc, array) => {
acc.push(...array);
return acc;
}, []);
You can use array.flat
let a = [
[{
a: 1,
b: 2
}, {
a: 1,
b: 3
}],
[{
a: 5,
b: 2
}, {
a: 2,
b: 5
}]
];
let b = a.flat();
console.log(b)
Alternatively you can use reduce and inside callback use forEach and puch items from the nested array to accumulator array
let a = [
[{
a: 1,
b: 2
}, {
a: 1,
b: 3
}],
[{
a: 5,
b: 2
}, {
a: 2,
b: 5
}]
];
let b = a.reduce((acc, curr) => {
curr.forEach(item => acc.push(item))
return acc;
}, []);
console.log(b)
use reduce() + push which faster flat() method.
refer this for to check performance. : https://jsbench.me/0ikcqa83ck/1
let arr = [
[{a: 1, b: 2},{a: 1, b: 3} ],
[{a: 5, b: 2},{a: 2, b: 5}]
]
console.log(arr.flat())
let flattenArr = arr.reduce((acc, val) => (acc.push(...val),acc), [])
console.log(flattenArr);
Array.flat is the magical solution you are looking for !
var arr = [
[{a: 1, b: 2},{a: 1, b: 3} ],
[{a: 5, b: 2},{a: 2, b: 5}]
]
console.log(arr.flat())

Sorting each object's key in an array of objects [duplicate]

This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 4 years ago.
I have the following array of object :
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{b: 2, a: 2},{d: 2, a: 2},{a: 2, b: 2}]
and I would like to sort each object by keys so that it will become:
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{a: 2, b: 2},{a: 2, d: 2},{a: 2, b: 2}]
I have tried the following code:
//sort keys
wordSpecsArr.forEach((obj) => {
var ordered = {};
Object.keys(obj).sort().forEach((key) => {
ordered[key] = obj[key]
});
});
But it did not work. I would really appreciate any help
Try this.
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{b: 2, a: 2},{d: 2, a: 2},{a: 2, b: 2}]
var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{a: 2, b: 2},{a: 2, d: 2},{a: 2, b: 2}]
//sort keys
let op =[];
wordSpecsArr.forEach((obj) => {
let ordered = {};
Object.keys(obj).sort().forEach((key) => {
ordered[key] = obj[key]
});
op.push(ordered)
});
console.log(op)

Select one property from Map.values()

I would like to have something like this:
let myMap = new Map<string, any>();
myMap.set("aaa", {a: 1, b: 2, c:3});
myMap.set("bbb", {a: 1, b: 2, c:6});
myMap.set("ccc", {a: 1, b: 2, c:9});
let cs = myMap.values().map(x => x.c);
Selecting the c property from all entries from the map. This is failing with:
Property 'map' does not exist on type 'IterableIterator<any>'.
Any elegant solution for this?
You can use Array.from() to turn any iterable into an array:
let myMap = new Map();
myMap.set("aaa", {a: 1, b: 2, c:3});
myMap.set("bbb", {a: 1, b: 2, c:6});
myMap.set("ccc", {a: 1, b: 2, c:9});
// Basic example
let cs = Array.from( myMap.values() ).map(x => x.c);
console.log( cs );
// Array.from also takes a mapping function as the second parameter, so even shorter:
let cs_alt = Array.from( myMap.values(), x => x.c );
console.log( cs_alt );

Convert object to multi-dimensional array - JavaScript

I have an object like this:
var myObj = {
a: 1,
b: 2,
c: 3,
d: 4
};
And i want to convert that object to a multi-dimensional array like this:
var myArray = [['a', 1], ['b', 2], ['c', 3], ['d', 4]];
How could i achieve this?
You can use Object.entries function.
var myObj = { a: 1, b: 2, c: 3, d: 4 },
myArray = Object.entries(myObj);
console.log(JSON.stringify(myArray));
...or Object.keys and Array#map functions.
var myObj = { a: 1, b: 2, c: 3, d: 4 },
myArray = Object.keys(myObj).map(v => new Array(v, myObj[v]));
console.log(JSON.stringify(myArray));
var myArray = [];
var myObj = { a: 1, b: 2, c: 3, d: 4 };
for(var key in myObj) {
myArray.push([key, myObj[key]]);
}
console.log(JSON.stringify(myArray));

Return an Array of Arrays containing objects that share a common value in a property

Say I have an array of 3 objects like this:
[
{
a: 4,
b: 5,
c: 4
},
{
a: 3,
b: 5,
c: 6
},
{
a: 2,
b: 3,
c: 3
}
]
I would like to return an array of arrays containing the objects that share a common value for the property b. So the resulting array would contain only one array containing 2 objects like this:
[
[
{
a: 4,
b: 5,
c: 4
},
{
a: 3,
b: 5,
c: 6
}
]
]
How would I do this?
You could do this with map and filter
var data = [{"a":4,"b":5,"c":4},{"a":3,"b":5,"c":6},{"a":2,"b":3,"c":3}];
var check = data.map(e => {return e.b});
var result = [data.filter(e => { return check.indexOf(e.b) != check.lastIndexOf(e.b)})];
console.log(result)
To group multiple objects in separate arrays with same b values you can use map and forEach
var data = [{"a":4,"b":5,"c":4},{"a":3,"b":5,"c":6},{"a":2,"b":3,"c":3}, {"a":3,"b":7,"c":6},{"a":2,"b":7,"c":3}], result = [];
var check = data.map(e => {return e.b});
data.forEach(function(e) {
if(check.indexOf(e.b) != check.lastIndexOf(e.b) && !this[e.b]) {
this[e.b] = [];
result.push(this[e.b]);
}
(this[e.b] || []).push(e);
}, {});
console.log(result)
This proposal uses a single loop with Array#forEach but without Array#indexOf.
var array = [{ a: 4, b: 5, c: 4 }, { a: 3, b: 5, c: 6 }, { a: 2, b: 3, c: 3 }],
grouped = [];
array.forEach(function (a) {
this[a.b] = this[a.b] || [];
this[a.b].push(a);
this[a.b].length === 2 && grouped.push(this[a.b]);
}, Object.create(null));
console.log(grouped);
You can create a function that accepts fulfillment criteria and will return as many nested arrays as rules passed.
Let's say you have an array of objects, arr.
var arr = [{a: 1, b: 2}, {a: 3, b: 2}, {a: 3, b: 4}, {a: 1, b: 1}]
And you want to return an array with with nested arrays that fulfill a particular requirement, let's say you want objects with an a:1 and b:2.
You can create a function that loops through your rules and creates a nested array with the objects that fulfill each rule.
For example:
var arr = [{a: 1, b: 2}, {a: 3, b: 2}, {a: 3, b: 4}, {a: 1, b: 1}]
function makeNestedArrays() {
var rules = [].slice.call(arguments);
return rules.reduce(function(acc, fn) {
var nestedArr = [];
arr.forEach(function(obj) {
if (fn(obj)) {
nestedArr.push(obj);
}
});
// only push nested array
// if there are matches
if (nestedArr.length) {
acc.push(nestedArr);
}
return acc;
}, []);
}
var result = makeNestedArrays(
function(obj) { return obj.a === 1; },
function(obj) { return obj.b === 2; }
);
console.log(result);
This allows you to pass as many "rules" as you want, and will create a nested array for each rule so long as there is at least one match.
You could use a Map to group them, this should work with any kind of value (just be sure the equality rules check out):
var arr = [{
a: 4,
b: 5,
c: 4
}, {
a: 3,
b: 5,
c: 6
}, {
a: 2,
b: 3,
c: 3
}];
var result = arr.reduce(function(m, o){
var value = o.b;
if(m.has(value)){
m.get(value).push(o);
} else {
m.set(value, [o]);
}
return m;
}, new Map());
console.log(...(result.values()));
If you'd need to filter out the groups of 1:
var arr = [{
a: 4,
b: 5,
c: 4
}, {
a: 3,
b: 5,
c: 6
}, {
a: 2,
b: 3,
c: 3
}];
var result = arr.reduce(function(m, o){
var value = o.b;
if(m.has(value)){
m.get(value).push(o);
} else {
m.set(value, [o]);
}
return m;
}, new Map());
result = [...result.values()].filter(a => a.length > 1);
console.log(result);

Categories

Resources