1

How can i fetch array which is inside of an array at different indexes.

Here my const variable X = [[{..}],[{..}]] contain values at different arrays inside of array X

[[{0:a}] , [{0:b}] , [{0:c}]]
const x = [[{0:a}] , [{0:b}] , [{0:c}]]
let y = null;
let index = 1;
y = x[index] //it does not get array from x first index array...

y = x[index]
//output [{…}]0: {0: -1}length: 1__proto__: Array(0)
I want to get this as output => [{0:b}]

I have const index = 1; How can I get index 1 array into const z = null initially const z is initialized as nul

puru
  • 150
  • 2
  • 12
  • 2
    Please update your post, the pseudocode you're showing has an object at `X[1][0]`, not an array. Also, the code you show says the error is on the `console.log`, but then your text says it's on the line _after_ that: which one is it? So, getting us to [how to ask a good question](/help/how-to-ask): please don't show pseudocode, actually show [mcve] code instead. – Mike 'Pomax' Kamermans Jun 14 '20 at 15:24
  • @Mike I just update the question – puru Jun 14 '20 at 15:34
  • const variable can't be reinitialized https://stackoverflow.com/a/23436563/13647574 so firstly change your const to var/let and other part of your question you have to give some proper example – MUHAMMAD ILYAS Jun 14 '20 at 15:45
  • Please update your post again after clicking and (re)reading how to ask a good question: you've now removed the code that was useful, and updated the code that wasn't to something that still isn't very useful. Just write actual code that folks can run: `const x = [ [{a:1}], [{b:2}] ];`, there, now you have a working representation of `x`, just add more code that shows how you're trying to get to what you're trying to get out of `x`, and then people can comment on how close you are to making that work. – Mike 'Pomax' Kamermans Jun 14 '20 at 17:18

1 Answers1

0

I hope I understood your question correctly. You want the the variable y to equal x[index] right?

I tried your code out and everything seems to be working fine here:

Perhaps the issue is that you are using the const variable, try using let or var like previous replies have emphasized. However, you aren't re-assigning the const variable so not sure how that could be the problem, but I might be mistaken. Regardless, this code below works, so do comment if that doesn't solve your issue. :))

const x = [[{0:"a"}] , [{0:"b"}] , [{0:"c"}]]
let y = null;
let index = 1;
y = x[index];
y = x[index];

console.log(...y);

The output is:

{0: "b"}

Is this what you want?

lukact
  • 50
  • 11