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);
Related
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 );
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);
I would like to use lodash to generete object from array of arrays. I use for it lodash.zipObject method and map. But it is not 1:1 what I would like to generate:
Input:
"rows": [
[
"stravi/aa",
"202001",
"59",
"51",
"2558.98",
"0.5358894453719162",
"1.9204668112983725",
"140",
"2.3466309084813943"
],
[
"stravi/ab",
"202003",
"3591",
"349",
"2246.09",
"0.41838214",
"3.57603358",
"50",
"4.82115474"
],
[
"stravi/ac",
"202007",
"3354",
"25",
"1975.76",
"0.74220667708",
"1.12321555541",
"11",
"0.9324532454"
]
]
dictionary: ['source', 'sessions', 'adClicks', 'adCost', 'CPC', 'CTR', 'goalCompletionsAll', 'goalConversionRateAll' ], [action.yearReportData]
output:
{
source: ['stravi/aa', 'stravi/ab', 'stravi/ac'],
sessions: ['202001', '202003', '202007']
...
}
I would like to use lodash, and I try by:
lodash.map(rows, arr =>
lodash.zipObject(['source', 'sessions', 'adClicks', 'adCost', 'CPC', 'CTR', 'goalCompletionsAll', 'goalConversionRateAll'], arr))
But is not correct... I received multiple object. I would like to have one object with all data. Like my example.
Unzip the rows to transpose the sub-arrays, and then use zip object:
const { flow, unzip, zipObject } = _
const fn = flow(
unzip,
arr => zipObject(['source', 'sessions', 'adClicks', 'adCost', 'CPC', 'CTR', 'goalCompletionsAll', 'goalConversionRateAll'], arr)
)
const rows = [["stravi/aa","202001","59","51","2558.98","0.5358894453719162","1.9204668112983725","140","2.3466309084813943"],["stravi/ab","202003","3591","349","2246.09","0.41838214","3.57603358","50","4.82115474"],["stravi/ac","202007","3354","25","1975.76","0.74220667708","1.12321555541","11","0.9324532454"]]
const result = fn(rows)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
why use third-party library, if may easy do it like so
let result = rows.reduce((obj, arr) => {
arr.forEach((item, i) => {
if (!obj[dictionary[i]]) {
obj[dictionary[i]] = [];
}
obj[dictionary[i]].push(item);
})
return obj;
}, {});
You need a reducer instead of map. Map just transform each elements that is why you get multiple elements.
I would use plain JS for this one. It will look like this:
const rows = [
[
'stravi/aa',
'202001',
'59',
'51',
'2558.98',
'0.5358894453719162',
'1.9204668112983725',
'140',
'2.3466309084813943',
],
[
'stravi/ab',
'202003',
'3591',
'349',
'2246.09',
'0.41838214',
'3.57603358',
'50',
'4.82115474',
],
[
'stravi/ac',
'202007',
'3354',
'25',
'1975.76',
'0.74220667708',
'1.12321555541',
'11',
'0.9324532454',
],
];
const keys = [
'source',
'sessions',
'adClicks',
'adCost',
'CPC',
'CTR',
'goalCompletionsAll',
'goalConversionRateAll',
'missingFieldName',
];
const initialAcc = keys.reduce((acc, cur) => {
acc[cur] = [];
return acc;
}, {});
const resultAcc = rows.reduce((acc, cur) => {
cur.forEach((value, index) => acc[keys[index]].push(value));
return acc;
}, initialAcc);
console.log(resultAcc);
I have an array of objects that I need to reformat into a list of arrays in a specific format.
I need my list to be formatted like this
list: [
[ "B", "A" ],
[ "F", "E" ],
]
But the closest I have come is this
list: ["B A", "F E"]
using this code
const itemList = [
{"ProductName":"A",
"Sku":"B",},
{"ProductName":"E",
"Sku":"F",}
];
const newList = itemList.map(item => `${item.Sku} ${item.ProductName}`);
console.log(newList);
How would I map this correctly?
You can create array with the values inside map:
const itemList = [
{"ProductName":"A",
"Sku":"B",},
{"ProductName":"E",
"Sku":"F",}
];
const newList = itemList.map(item => [item.Sku, item.ProductName]);
console.log(newList);
You can also use destucure for each item and map it to array of these values:
const itemList = [
{
ProductName: 'A',
Sku: 'B'
},
{
ProductName: 'E',
Sku: 'F'
}
];
const newList = itemList.map(({ProductName, Sku}) => [
Sku,
ProductName
]);
console.log(newList);
To keep things simple, I would use Object.values as such:
const newList = [];
itemList.map(item => newList.push(Object.values(item)));
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);