how to create a array of values from dictionary having specific key? - javascript

Input
dict = [{a: 1, b: 2, c: 3},{a: 4, b: 5, c: 6}, {a: 7, b: 8, c: 9} ]
Output to array having only values of key 'a' in typescript
array = [ 1, 4, 7 ]

Here is how you can use the Array.prototype.map() function:
> dict = [ { a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, { a: 7, b: 8, c: 9 } ]
[ { a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, { a: 7, b: 8, c: 9 } ]
> dict.map(({a}) => a)
[ 1, 4, 7 ]
>

Related

How to change index of elements in array of objects according to another array JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have two arrays and need to change the order of elements in a way to be similar. by property c. i should get index of object of property c: 2 for example and place it to the right index in arrB . JavaScript
const arrA= [{
a: 1,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
},
{
a: 2,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
}
];
const arrB = [{
a: 1,
b: [{
c: 5,
f: 6
},
{
c: 2,
d: 3
},
]
},
{
a: 2,
b: [{
c: 5,
f: 6
},
{
c: 2,
d: 3
},
]
}
];
What I need is to sort arrB in order of all elements in key b to have the same order like in arrA. in reality this object contain more fields and they aren't similar. but they have the same property c by which i should change the order
const arrB= [{
a: 1,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
},
{
a: 2,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
}
];
You can use sort() to order the elements based on the indexes of the matching elements in arrA[i].b.
const arrA = [{
a: 1,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
},
{
a: 2,
b: [{
c: 2,
d: 3
},
{
c: 5,
f: 6
}
]
}
];
const arrB = [{
a: 1,
b: [{
c: 5,
f: 6
},
{
c: 2,
d: 3
},
]
},
{
a: 2,
b: [{
c: 5,
f: 6
},
{
c: 2,
d: 3
},
]
}
];
arrB.forEach((item, i) =>
item.b.sort((el1, el2) => arrA[i].b.findIndex(el => el.c == el1.c) - arrA[i].b.findIndex(el => el.c == el2.c)));
console.log(arrB);

Finding all the keys in the array of objects

Given that I have an array that is return from database that contains object,
var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4, c: 66},
{a: 5, b: 6, c: 55, d: 66},
{a: 7, b: 8, c: 12, e: 15}
];
How can I get all the keys of the object? I've been using this to obtain the keys, however I notice that index 0 wont always have all the keys. Hence problem lays.
let columns = Object.keys(jsObjects[0]),
However, the first index won't always have all the columns.
My desired output:
["a", "b", "c", "d", "e"]
Sets are good at removing duplicates:
const jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4, c: 66},
{a: 5, b: 6, c: 55, d: 66},
{a: 7, b: 8, c: 12, e: 15}
];
const keys = [...new Set(jsObjects.flatMap(Object.keys))];
console.log(keys);
You could create an object with the count of the keys and get the keys from counts.
const
objects = [{ a: 1, b: 2 }, { a: 3, b: 4, c: 66 }, { a: 5, b: 6, c: 55, d: 66 }, { a: 7, b: 8, c: 12, e: 15 }],
result = Object.keys(objects.reduce((r, o) => {
Object.keys(o).forEach(k => r[k] = true);
return r;
}));
console.log(result);
You can use a Set(), which is like an array with the exception that it doesn't allow multiple occurances of elements.
var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4, c: 66},
{a: 5, b: 6, c: 55, d: 66},
{a: 7, b: 8, c: 12, e: 15}
];
var keys = new Set();
for(object of jsObjects) {
for(key in object) {
keys = keys.add(key);
}
}
for (const key of keys) {
console.log(key);
}
I tried this solution: iterating on jsObjects elements and pushing keys
into columns array if not present.
var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4, c: 66},
{a: 5, b: 6, c: 55, d: 66},
{a: 7, b: 8, c: 12, e: 15}
];
let columns = [];
jsObjects.forEach(
o => {
Object.keys(o).forEach(
t => {
if (columns.indexOf(t) < 0 ) {
columns.push(t);
}
}
)
}
)
console.log(columns);

Delete multiple keys from array of Object

Let's say we have an array of object:
var obj = [{a: 1, b: 2, c: 3, d: 4, e: 5 },{a: 6, b: 7, c:
8, d: 9, e: 0 }];
and we want to delete key c,e from both of the objects.
How can it be done? One of the methods I found is:
['c', 'e'].forEach(e => delete obj[e]); //for object
Is there any other way so we don't have to use double for loop.
One way to do it is to use .map() together with object destructuring:
var obj = [
{ a: 1, b: 2, c: 3, d: 4, e: 5 },
{ a: 6, b: 7, c: 8, d: 9, e: 0 },
];
var newObj = obj.map(({ c, e, ...rest }) => rest);
console.log(newObj)
This will create a new array with new objects which contain all of the other keys except for c and e.
You have 2 options to resolve it:
By using object destructuring: map(({ a,b,c,d,e }) => ({a,b,d})
Enhance option 1 by using using [Rest parameters] { c, e, ...rest }
Object destructuring like below
const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 } var {c, e} = obj; // c = 3, e = 5
With option 2, you will have c,e implicit name and the remaining items named rest. After that, you just need to get rest items.
Option 1
var obj =
[
{ a: 1, b: 2, c: 3, d: 4, e: 5 },
{ a: 6, b: 7, c: 8, d: 9, e: 0 },
];
console.log(obj.map(({ a,b,c,d,e }) => ({a,b,d})));
Option 2
var obj =
[
{ a: 1, b: 2, c: 3, d: 4, e: 5 },
{ a: 6, b: 7, c: 8, d: 9, e: 0 },
];
console.log(obj.map(({ c, e, ...rest }) => rest));
// ...rest: the same as `a,b,d`

How to "uniqueify" a javascript array of objects?

How does one remove objects in an array that match across all keys and values? I've seen variations of this question but only for a particular field.
For example, given the following input the output would look like so:
> var a = [
{a:1, b:'x'},
{a:1, b:'y'},
{a:1, b:'y'},
{a:2, b:'x'},
{a:2, b:'y'},
{a:2, b:'y'},
{a:2, b:'x'},
{b:'y', a:2},
{a:2, b:'y', c:'surprise!'}
]
> blackbox(a)
[
{a:1, b:'x'},
{a:1, b:'y'},
{a:2, b:'x'},
{a:2, b:'y'},
{a:2, b:'y', c:'surprise!'}
]
Ideally blackbox isn't hard-coded with the keys.
BTW, here the hack that I have now. It turns each (sorted) object into a string and checks to see if it's seen that string before.
Regardless, there's got to be a more elegant solution out there!
> function uniq(a) {
var keys = new Set();
return a.filter(function(row) {
var key = Object.entries(row).sort().toString();
var uniq = !keys.has(key);
keys.add(key);
return uniq;
});
}
> a =
[ { a: 1, b: 'x' },
{ a: 1, b: 'y' },
{ a: 1, b: 'y' },
{ a: 2, b: 'x' },
{ a: 2, b: 'y' },
{ a: 2, b: 'y' },
{ a: 2, b: 'x' },
{ b: 'y', a: 2 },
{ a: 2, b: 'y', c: 'surprise!' },
{ c: 'surprise!', a: 2, b: 'y' } ]
> uniq(a)
[ { a: 1, b: 'x' },
{ a: 1, b: 'y' },
{ a: 2, b: 'x' },
{ a: 2, b: 'y' },
{ a: 2, b: 'y', c: 'surprise!' } ]
> b =
[ { a: { b: 1, c: 2 }, b: 1 },
{ b: 1, a: { c: 2, b: 1 } },
{ a: { b: 1, c: 2 }, b: 2 } ]
> uniq(b) // works because nested objects happen to be identical
[ { a: { b: 1, c: 2 }, b: 1 }, { a: { b: 1, c: 2 }, b: 2 } ]
> c =
[ { a: { b: 1, c: 2 }, b: 1 },
{ b: 1, a: { c: 2, b: 1 } },
{ a: { b: 1, c: 2 }, b: 2 },
{ a: { b: 2, c: 1 }, b: 2 } ]
> uniq(c) // fail on nested object
[ { a: { b: 1, c: 2 }, b: 1 }, { a: { b: 1, c: 2 }, b: 2 } ]

Node.js object array data joins

Are there any libraries or efficient techniques to perform array joins in node JS such that,
A = [ { a: 1, b: 'a' }, { a: 2, b:'b' }, { a: 3, b: 'a' }, { a: 4, b: 'b' } ]
B = [ { a: 1, c: true }, { a: 2, c: true }, { a: 3, c: false } ]
could be joined such that the following results could be produced:
# Intersection on a
C = [ { a: 1, b: 'a', c: true }, { a: 2, b: 'b', c: true }, { a: 3, b: 'a', c: false } ]
# Union on a
D = [ { a: 1, b: 'a', c: true }, { a: 2, b: 'b', c: true }, { a: 3, b: 'a', c: false }, { a: 4, b: 'b' } ]
Is array.map the best solution to this problem?
efficiency is paramount here, since it could be handling huge arrays in production
You're not very specific about how you identify and merge your object.
Using Underscore, the result can be obtained as follow:
_u=require("underscore")
A = [ { a: 1, b: 'a' }, { a: 2, b:'b' }, { a: 3, b: 'a' }, { a: 4, b: 'b' } ]
B = [ { a: 1, c: true }, { a: 2, c: true }, { a: 3, c: false } ]
D = _u.zip(A,B).map(
function(x){
return _u.extend(x[0],x[1]);
}
);
C = _u.zip(A,B).filter(
function(x){
return !!x[1];
}
).map(
function(x){
return _u.extend(x[0],x[1]);
}
);
Is that what you're looking for ?

Categories