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
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
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', ...
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]);
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]);