#zip 2 arrays 1 obj
29 messages · Page 1 of 1 (latest)
it can, but the first set of curly braces is being recognized as a code block. also you can't have a variable as a key in that format.
/run js
const test0 = ['a', 'b', 'c'];
const test1 = [0, 1, 2];
const zip = test0.map((i, v) => {
let r = {};
r[v] = test1[v];
return r;
});
console.log(zip);
Here is your js(16.3.0) output @grand grail
[ { '0': 0 }, { '1': 1 }, { '2': 2 } ]
sorry I just want 1 obj with key:value pairs
like { a: 1, b:2}
ah then use foreach instead of map; define the object before the loop; and just do assignments inside the foreach
nice lets try
/run js
const test0 = ['a', 'b', 'c'];
const test1 = [0, 1, 2];
const r = {};
const zip = test0.forEach((i, v) => [v] = test1[v]
console.log(r);
it's /run js and forEach doesn't return anything
/run js
const test0 = ['a', 'b', 'c'];
const test1 = [0, 1, 2];
const r = {};
const zip = test0.forEach((i, v) => r[v] = test1[v])
console.log(r);
Here is your js(16.3.0) output @sacred cliff
{ '0': 0, '1': 1, '2': 2 }
try r[v] instead of just [v]
also without looking it up i'm 99% sure you have i and v backwards; first param is the value, 2nd is the index
ye ty alot ❤️
ye but don't work on my code
const columns = [data[0]];
const randomRow = [data[Math.floor(Math.random() * data.length)]];
const zip = {};
columns.forEach((col, index) => (zip[col] = randomRow[index]));
console.log(zip);
I using csv-parse to read a csv local
and turn into a 'dataframe'
i'm not sure you meant to wrap columns and randomRow in extra arrays
the extra square brackets
ye but typescript kidding me
without this ones
let me se
works without typescript string[] types
nice one ty alot man, how can I close this one?