-5

Lets Say I have

A = [cat,dog,horse]
B = [angry,happy,sad]

call = {}

How can I make json object which looks like this

console.log(call.animal[2]) // horse

console.log(call.mood[2]) // sad
azuldev
  • 570
  • 6
  • 10
  • 1
    `call = { animal : A, mood : B}` ? or `call = {}; call.animal = A; call.mood = B` ? – Code Maniac Sep 16 '19 at 01:45
  • Are you asking how to turn this into JSON or how to merge two arrays into an object? Read https://stackoverflow.com/questions/4215737/convert-array-to-object – Mr. J Sep 16 '19 at 01:51

3 Answers3

1

Put a as the animal property of call, and b as the mood property of call:

call = { animal: A, mood: B };

If the arrays are meant to be of strings, then make sure to put ' around each string, like

A = ['cat', ...
Snow
  • 3,820
  • 3
  • 13
  • 39
0

As easy as this. This is a new feature of es6, animals and emotions variables become a key-value (variable name and its value) in an object.

const animals = ['cat','dog','horse'];
const emotions = ['angry','happy','sad'];

const call = {animals, emotions};

console.log(call.animals[2], call.emotions[2]);
Joven28
  • 769
  • 3
  • 12
0

I assume that your array is string.

const a = ['cat','dog','horse'];
const b = ['angry','happy','sad'];

call = {
  animal : a,
  fruit : b
}

console.log(call.animal[1]);