How to get same value from array of objects using javaScript? - javascript

I have two arrays, my aim is to filter the second array arr2 by the first array arr1, and get only matched values where prid is the same as arr2 id, results should be let filtred = [ {name:'kiwi', color:'red', id:12123},{name:'kiwi', color:'red', id:12123}], how i can ichieve this using filter?
let arr1 = [
{prid:12123},
{prid:12124}
]
let arr2 = [
{name:'kiwi', color:'red', id:12123},
{name:'kiwi2', color:'blue',id:12129},
{name:'kiwi3', color:'green',id:12124}
]

Please note: According to your requirement your expected output is not correct:
You can try using Array.prototype.some() inside Array.prototype.filter() by matching the respective property value (prid of arr1 with id of arr2).
let arr1 = [
{prid:12123},
{prid:12124}
]
let arr2 = [
{name:'kiwi', color:'red', id:12123},
{name:'kiwi2', color:'blue',id:12129},
{name:'kiwi3', color:'green',id:12124}
]
let filtred = arr2.filter(i => arr1.some(j => j.prid == i.id))
console.log(filtred);

You can use Array.prototype.reduce which allows to create another array with different structure
let arr1 = [
{prid:12123},
{prid:12124}
]
let arr2 = [
{name:'kiwi', color:'red', id:12123},
{name:'kiwi2', color:'blue',id:12129},
{name:'kiwi3', color:'green',id:12124}
]
let results = arr2.reduce((accumulator, currentItem) =>{
let itemExists = arr1.findIndex(item => item.prid === currentItem.id);
return itemExists !== -1? accumulator.concat(currentItem): accumulator;
}, []);
console.log(results);

Related

How to make the nested items of array to object?

I have a nested array like this:
var arr = [
['Simcard', 'one'],
['Charge', 'two'],
];
I want to make it like this:
[
{"Simcard": "one"},
{"Charge": "two"},
]
How can I do that?
Here is what I've tried but not exactly the expected result:
let res = Object.keys(x).map((key) => {key: x[key]});
There's a builtin Object.fromEntries that turns a two-element array into an object.
var arr = [
['Simcard', 'one'],
['Charge', 'two'],
];
var desired = [
{"Simcard": "one"},
{"Charge": "two"},
]
var answer = arr.map(entry => Object.fromEntries([entry]));
console.log(answer)
This is not a oneliner, but you could try the following
let res = []
arr.forEach(element => res[element[0]] = element[1])
let res = arr.map(item => ({[item[0]]: item[1]}))
You can use Array#map as follows. I think you biggest challenge was having the key in a variable: so that the word key does not become the key but it's value we use [key] instead.
const arr = [ ['Simcard', 'one'], ['Charge', 'two'] ],
res = arr.map(([key,value]) => ({[key]: value}));
console.log( res );

Replace an object that is inside of array if object id value from another array match

I have this arrays with objects that looks like this:
array1 = [
0:{id:145, value:130000},
1:{id:146, value:103300},
2:{id:147, value:79500},
]
array2 = [
0:{id:145, value:135000}
]
And I want to replace the object inside the array if the id of the object in array2 match with some id of the object in array1
So I expect something like this:
array1 = [
0:{id:145, value:135000},
1:{id:146, value:103300},
2:{id:147, value:79500},
]
I have this code
array1.splice(1, 1, array2[0])
but it returns me this:
array1 = [
0:{id:145, value:135000},
1:{id:145, value:130000},
2:{id:146, value:103300},
3:{id:147, value:79500},
]
Any help I'll appreciate
let array1 = [
{id:145, value:130000},
{id:146, value:103300},
{id:147, value:79500},
]
let array2 = [
{id:145, value:135000},
{id:147, value:135023}
]
array2.map(x => {
let index = array1.findIndex(d=> d.id === x.id)
array1[index] = x
})
console.log(array1)
array2.forEach(i1 => {
const index = array1.findIndex(i2 => i2.id == i1.id);
if(index > -1) {
array1.splice(index, 1, i1);
}
});

How to make array of objects from two another arrays?

I have two arrays.
let arr1 = ['dog', 'cat', 'mouse']
let arr2 = ['dog']
How to create another third array from the result?
let arr3 = [{dog: true}, {cat: false}, {mouse: false}]
I can't find a way how to create keys from these string?
Map the first array, and use computed property names to generate the keys from the current array item. Generate the value by checking if the 2nd array includes the current key.
const arr1 = ['dog', 'cat', 'mouse']
const arr2 = ['dog']
const result = arr1.map(key => ({
[key]: arr2.includes(key)
}))
console.log(result)

Insert Object were match array

I have two array arr1 where qid is unique and arr2 where markid is unique. I want match qid and markid and result from that result arr3
arr1=[subjectwisequetion:[questions:
[{qid:"1",anskey:"c"}],
[{qid:"2",anskey:"c"}],
[{qid:"3", anskey:"c"}]
]
]
arr2=[
{markid:"2", markstatus:"2"},
{markid:"3", markstatus:"1"}
]
arr3= [subjectwisequetion:
[questions:[
{qid:"1", anskey:"c", markstatus:"null"}
],
[
{qid:"2", anskey:"c", markstatus:"2"}
],
[
{qid:"3", anskey:"c" , markstatus:"1"}
]
]
]
try this, it works for me:
let arr11 = [{qid:"1", anskey:"c"}, {qid:"1",anskey:"c"},{qid:"1", anskey:"c"}]
let arr22 = arr2=[ {markid:"2", markstatus:"2"},{markid:"3", markstatus:"1"}]
let arr33 = arr11.map((item, i) => Object.assign({}, item, arr22[i]));

How to sort a list on another list in lodash

Gived a list
[
{id:'i1',value:'v1'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i2',value:'v2'},
{id:'i5',value:'v5'},
]
and another ordered id list
['i1','i2','i3','i4','i5']
get
[
{id:'i1',value:'v1'},
{id:'i2',value:'v2'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i5',value:'v5'},
]
How to do this in lodash?
Use sortBy method and return the id's index position from the iteratee function
let sortedArray = _.sortBy(input, (v) => order.indexOf(v.id))
where
input = [
{id:'i1',value:'v1'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i2',value:'v2'},
{id:'i5',value:'v5'},
]
order = ['i1','i2','i3','i4','i5']
Convert Array to Object where key is id for better lookup. Replace each element in ordered list with actual value.
Edge case: the two array has the same elements
const foo = [
{id:'i1',value:'v1'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i2',value:'v2'},
{id:'i5',value:'v5'},
];
const ordered = ['i1','i2','i3','i4','i5'];
const fooMap = _.keyBy(f, 'id');
const orderedFoo = ordered.map(id => fooMap[id])
const arr = ['i1','i2','i3','i4','i5'];
const arrObj = [
{id:'i1',value:'v1'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i2',value:'v2'},
{id:'i5',value:'v5'}
]
const sortedArr = [];
_.map(arr, a => sortedArr.push(_.find(arrObj, ['id', a])));
console.log(sortedArr)
let arr = ['i1','i2','i3','i4','i5'];
let arrObj = [
{id:'i1',value:'v1'},
{id:'i3',value:'v3'},
{id:'i4',value:'v4'},
{id:'i2',value:'v2'},
{id:'i5',value:'v5'}
]
console.table(arrObj);
arr.map(v => {
arrObj.sort( v1 => { return v1.id == v ? true : false} )
})
console.table(arrObj);

Categories

Resources