0

This is driving me mad, I have a JSON string that looks like so:

[["OX", "139.38"], ["LE", "131.28"], ["SA", "105.45"]]

I want to convert this to an array so that I can I can take advantage of indexOf:

alert(myArray.indexOf("LE"));

I've tried to convert my JSON string to an array using JSON.parse(myJSON) and jQuery.parseJSON(myJSON) but neither work.

How can I create an array from my JSON string? I'm happy to set up a loop just not sure how.

5
  • Do you have anything to change the JSON? Commented Aug 18, 2015 at 10:23
  • but neither work - don't work how? What happens instead? Commented Aug 18, 2015 at 10:24
  • They say invalid characters. I think the JSON string isn't a "true" JSON string Commented Aug 18, 2015 at 10:25
  • Include the errors you are getting, what they are related to, and the actual string. What youve included above isnt a string but an array. Whats not working specifically? also given that you have an array of arrays you would have to do myArray[1].indexOf('LE'); Commented Aug 18, 2015 at 10:35
  • Do you want to convert it like so: [[""OX", "139.38"], ["LE", "131.28""]] -> ["OX", "139.38", "LE", "131.28"] or ["OX", "LE"]? Commented Aug 18, 2015 at 10:45

3 Answers 3

1

This works in chrome console:

var s = '[["OX", "139.38"], ["LE", "131.28"], ["SA", "105.45"]]';
var a = JSON.parse(s);

// a = [["OX", "139.38"],["LE", "131.28"],["SA", "105.45"]]

If you want to create a data structure to lookup the values by the keys:

var dict = {};
for (var i=0; i<a.length; i++){
    dict[a[i][0]] = a[i][1];
}

Now dict looks like this:

//dict = {OX:"139.38", LE:"131.28", SA:"105.45"}

and we can index into it with the keys

dict['LE'] // = "131.28"
Sign up to request clarification or add additional context in comments.

1 Comment

And you can do a[0].indexOf("OX") // => 0 -> The a[0] references the first Array within the overall array. Just FYI this is essentially what is called a multidimensional array
0

This will convert it like this [[""OX", "139.38"], ["LE", "131.28""]] -> ["OX", "LE"]

var newArr = [];

for(var i = 0; i < myArr.length; i++) {
    newArr.push(myArr[i][0]);
}

This will convert it like this [[""OX", "139.38"], ["LE", "131.28""]] -> ["OX", "139.38", "LE", "131.28"]

var newArr = [];

for(var i = 0; i < myArr.length; i++) {
    var tempArr = myArr[i];
    for(var j = 0; j < tempArr.length; j++) {
        newArr.push(tempArr[j]);
    }
}

Now you can use indexOf.

Comments

0

If you want to get index at outter array:

var x = '[["OX", "139.38"], ["LE", "131.28"], ["SA", "105.45"]]';
var y = JSON.parse(x);

function getIndexByKey(key,array){
  for(var i=0, len = array.length; i<len;i++){
    if(array[i][0] === key){
      return i;
    }
  }
}

function getIndexByVal(val,array){
  for(var i=0, len = array.length; i<len;i++){
    if(array[i][1] === val){
      return i;
    }
  }    
}

calling it:

getIndexByKey('OX', y); //0
getIndexByKey('LE', y); // 1
getIndexByKey('SA', y)  // 2

getIndexByVal('139.38', y); //0
...

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.