#zip 2 arrays 1 obj

29 messages · Page 1 of 1 (latest)

sacred cliff
#

!!run


  const test0 = ['a', 'b', 'c'];
  const test1 = [0, 1, 2];

  const zip = test0.map((i, v) => {
    v: test1[i];
  });
  console.log(zip);
#

why my key v can't be a key for a object?

grand grail
#

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);
forest shuttleBOT
#

Here is your js(16.3.0) output @grand grail

[ { '0': 0 }, { '1': 1 }, { '2': 2 } ]
sacred cliff
#

like { a: 1, b:2}

grand grail
#

ah then use foreach instead of map; define the object before the loop; and just do assignments inside the foreach

sacred cliff
#

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);
grand grail
#

it's /run js and forEach doesn't return anything

sacred cliff
#

/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);
forest shuttleBOT
#

Here is your js(16.3.0) output @sacred cliff

{ '0': 0, '1': 1, '2': 2 }
grand grail
#

try r[v] instead of just [v]

sacred cliff
#

yei ❤️

#

/close

grand grail
#

also without looking it up i'm 99% sure you have i and v backwards; first param is the value, 2nd is the index

sacred cliff
#

ye ty alot ❤️

grand grail
#

which is why you're getting indices for keys

#

yw

sacred cliff
#
  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'

grand grail
#

i'm not sure you meant to wrap columns and randomRow in extra arrays

#

the extra square brackets

sacred cliff
#

ye but typescript kidding me
without this ones
let me se

#

works without typescript string[] types

sacred cliff