Javascript: how to access each element in an array

时间:2016-07-11 23:01:38

标签: javascript jquery json indexing

I query two arrays from the database and turn them into json format. The set up is like this:

{"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"}

I am trying to access each element in groups. The groups array is a list. I tried using index, and it's returning me groups[0] = 'a', groups[1] = 'p'... So using index doesn't work.

I also want to count how many elements in the groups array or in the members array, and using .length only give me back the character length, not the actual numbers of elements.

Any advice would be appreciated. Thanks.

4 个答案:

答案 0 :(得分:4)

JSON.parse(groups) will not work, because [apple,bee,car,dogs,egos,fruits] is not correct JSON string.

["apple","bee","car","dogs","egos","fruits"] - is correct JSON string, that can be parsed.

P.S. members is not correct JSON string too.

// If you have some data

data = {
  groups: ["apple", "bee", "car", "dogs", "egos", "fruits"],
  members: ["g", "h", "i", "j", "k", "l"]
};

// you can convert it to JSON string

jsonData = JSON.stringify(data);
console.log('JSON data: ', jsonData);

// and then parse this string

restoredData = JSON.parse(jsonData);

// after this you can access object members again

console.log('groups[0]: ', restoredData.groups[0]);

答案 1 :(得分:1)

This is happening because "[apple,bee,car,dogs,egos,fruits]" it's an string. You have to parse it before accessing the element.

Let's say that you have the JSON in the variable test, then we have to delete [ and ] and split the string like this:

test.groups = test.groups.replace("[", "")
test.groups = test.groups.replace("]", "")
test.groups = test.groups.split(',')

And then now it contains:

["apple", "bee", "car", "dogs", "egos", "fruits"]

答案 2 :(得分:0)

You should consider constructing the array differently from the database.

You are getting those letters because they are in the position in the string that you are referencing with the index.

Consider using regex and the split() function to parse them as they are now:

var obj = {"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"};

// Regex replaces the square brackets with blank spaces and splits by the
// comma into an array
var groups = obj.replace(/[\[\]']+/g,'').split(',');
var members = obj.replace(/[\[\]']+/g,'').split(',');

Now groups[1] will return 'bee'.

Read more about split() here.

答案 3 :(得分:0)

它不起作用,因为数组的所有元素都在一个字符串中。

正如已经提到的,这不是正确的JSON格式,但有时您无法控制如何获取信息。因此,虽然这不是推荐的答案,但您可以使用简单的字符串解析来获取您想要的值,例如:

var stuff = JSON.parse('{"groups":"[apple,bee,car,dogs,egos,fruits]", "members":"[g,h,i,j,k,l]"}');

var groups = stuff.groups.split("[")[1].split("]")[0].split(",");
var members = stuff.members.split("[")[1].split("]")[0].split(",");

console.log(groups); // ["apple", "bee", "car", "dogs", "egos", "fruits"]
console.log(groups[1]); //bee
console.log(members[0]); //g

我想重申这不是一个理想的解决方案,但有时你只能这样做。

相关问题