I have the next code:
JavaScript
x
12
12
1
let dataList = [];
2
3
function filli(index, name, words){
4
dataList[index] = index;
5
dataList[index] = name;
6
dataList[index] = words;
7
}
8
9
filli(0, "David", "Testing this")
10
filli(1, "John", "My cellphone")
11
console.log(dataList)
12
I get in console the next:
JavaScript
1
3
1
$ node attempt.js
2
[ 'Testing this', 'My cellphone' ]
3
And my expected output in console would be:
JavaScript
1
2
1
[[0, "David", "Testing this"], [1, "John", "My cellphone"]]
2
But I don´t get that in console, as you can see I was trying to get that data in my filli
function, sending an index, name, and words but it doesn’t work.
I hope you can help me, thank you.
Advertisement
Answer
You are overriding existing values when you do dataList[index]= ...
. You need to add them as an array, something like this:
JavaScript
1
10
10
1
let dataList = [];
2
3
function filli(index, name, words){
4
dataList[index] = [index, name, words];
5
}
6
7
filli(0, "David", "Testing this")
8
filli(1, "John", "My cellphone")
9
console.log(dataList)
10