Insert Object were match array - javascript

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]));

Related

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);
}
});

Split array elements dynamically

I have below scenarios to handle.
let data = [
[ "ALISHA", "SUICA", "PASMO" ],
[ "HARMONY" ],
[ "OCTOPUS" ]
]
let data1 = [
[ "ALISHA", ],
[ "HARMONY" ],
[ "OCTOPUS", "SUICA", "PASMO" ]
]
For both of the above data, i want the result to look like this.
let result = [
[ "ALISHA" ],
[ "HARMONY" ],
[ "OCTOPUS" ],
[ "SUICA" ],
[ "PASMO" ]
]
Can someone please let me know how to achieve this. I tried the following but no success
let result = []
for (let i = 0; i < data.length; i++) {
let split = data[i].split(","); // just split once
result.push(split[0]); // before the comma
}
we will use forEach method on main array inside forEach we will use if condition if is array and length more than 1 will add another forEach method and push sub array to main array after that remove sub array
look like that
let data = [
["ALISHA"],
["HARMONY"],
["OCTOPUS", "SUICA", "PASMO"]
]
data.forEach((cur, index) => {
if (Array.isArray(cur) && cur.length > 1) {
cur.forEach(cur => data.push([cur]))
data.splice(index, 1);
}
})
console.log(data)
Uses Array.reduce extract all elements, then convert to [string] by Array.map.
const data = [
[ "ALISHA" ],
[ "HARMONY" ],
[ "OCTOPUS", "SUICA", "PASMO" ]
]
console.log(
data.reduce((pre, cur) => [...pre, ...cur], []).map(item => [item])
// data.reduce((pre, cur) => pre.concat(...cur), []).map(item => [item]) // different methods but same logic (uses Array.concat instead of spread operator)
)
You can use flat and map
const data = [["ALISHA"], ["HARMONY"], ["OCTOPUS", "SUICA", "PASMO"]];
const result = data.flat().map((a) => [a]);
console.log(result);

How to get same value from array of objects using 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);

Concat all values inside a map

I've got a constant result like this :
result: Map<string, string[]>
When I do a console.log(result) the output is :
Map {
'toto' => [ 'a-1', 'a-2' ],
'tata' => [ 'b-1', 'b-2' ],
'titi' => [ 'c-1', 'c-2' ],
}
What I want to have, it's a constant globalResult with all values like this:
const globalResult = [ 'a-1', 'a-2','b-1','b-2','c-1','c-2' ]
How can I do this ?
Thanks
You can get map values into an array and then use flat() method on it like:
const myMap = new Map().set('toto', ['a-1', 'a-2']).set('tata', ['b-1', 'b-2'])
const arr = [...myMap.values()].flat()
console.log(arr)
You can use Array.from to covert map values into a flat array
const map = new Map();
map.set('a',11)
map.set('b',22)
map.set('c',33)
const array = Array.from(map.values())
console.log(array)
You could get the values of the properties and flat the arrays.
const
object = { toto: ['a-1', 'a-2'], tata: ['b-1', 'b-2'], titi: ['c-1', 'c-2'] },
array = Object.values(object).flat();
console.log(array);
Use forEach function.
const obj = {
'toto' : [ 'a-1', 'a-2' ],
'tata' : [ 'b-1', 'b-2' ],
'titi' : [ 'c-1', 'c-2' ],
}
const arr = [];
Object.values(obj).forEach(value=>arr.push(...value));
console.log(arr);

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