1

I have the following array stored at the front end.

myArr[0] = new Array("AF", "B", "C", "D", "EM", "F", "G", "H", "I", "J");
myArr[1] = new Array("C", "DD", "M", "ED", "F", "DG", "B", "C", "D", "C");
myArr[2] = new Array("F", "G", "H", "I", "E", "F", "G", "H", "I", "J");

i need to get the value of the elements in an array. for example myArr[0][0] should return AF. unfortunately i am getting the position to be find of the array as string of array as follows from a service call.

["myArr[1][6]", "myArr[3][4]", "myArr[4][2]"]

how do i convert this and get the value at the array positions mentioned above. Thanks in advance.

3
  • How are you getting the second array? Commented Jul 11, 2019 at 10:25
  • Is the service that you call something you can amend? The response isn't ideal. You'd be better receiving the positions instead. Commented Jul 11, 2019 at 10:26
  • Possible duplicate of Accessing nested JavaScript objects with string key Commented Jul 11, 2019 at 10:32

4 Answers 4

3

You can use eval to execute strings as script:

eval("myArr[1][6]") // gives "C"

See codepen here: https://codepen.io/jenko3000/pen/NZJRYK

Be careful though, executing strings as scripts is dangerous.

Sign up to request clarification or add additional context in comments.

9 Comments

why the downvote? this is totally acceptable if it's safe to do it
Why did you post a codepen? Couldn't you use a stack snippet?
I guess, I just find codepens easier to work with. The snippets are horribly clunky.
Haha, exactly :P
@WillJenkins Awesome, that's me removed my -1. Sorry again. I wanted you to edit it because 10 minutes elapsed and my vote was locked until you edited.
|
1

You can use regex to "extract" the two indices and then use them to get the values like so:

let myArr = [];
myArr[0] = new Array("AF", "B", "C", "D", "EM", "F", "G", "H", "I", "J");
myArr[1] = new Array("C", "DD", "M", "ED", "F", "DG", "B", "C", "D", "C");
myArr[2] = new Array("F", "G", "H", "I", "E", "F", "G", "H", "I", "J");

let arr = ["myArr[1][6]", "myArr[2][4]", "myArr[0][2]"];

let res = arr.map(str => {
  let [x, y] = str.match(/(\d+)/g);
  return myArr[x][y];
});
console.log(res);

Comments

1

There's no need to over-complicate the issue. A simple map over each value and evaluate will suffice:

const myArr = [
  ["AF", "B", "C", "D", "EM", "F", "G", "H", "I", "J"],
  ["C", "DD", "M", "ED", "F", "DG", "B", "C", "D", "C"],
  ["F", "G", "H", "I", "E", "F", "G", "H", "I", "J"]
]

var positions = ["myArr[1][6]", "myArr[2][4]", "myArr[0][2]"]

positions = positions.map(eval)

console.log(positions)

Note, we can use the shorthand .map(eval) over .map(e => eval(e)) because we are only passing one element to one function, so the shorthand will do that for us.

Also, as Will pointed out, eval can be dangerous, as eval can execute malicious scripts.

Comments

0

This code will replace the positions stored in the array with their respective values.

let myArr=[]
myArr[0] = new Array("AF", "B", "C", "D", "EM", "F", "G", "H", "I", "J");
myArr[1] = new Array("C", "DD", "M", "ED", "F", "DG", "B", "C", "D", "C");
myArr[2] = new Array("F", "G", "H", "I", "E", "F", "G", "H", "I", "J");
let values=["myArr[1][6]", "myArr[2][4]", "myArr[0][2]"];
for (var i = 0; i < values.length; i++)
{
	 values[i]=eval(values[i])
}
console.log(values)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.