最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access JSON-object with multiple values per key? - Stack Overflow

programmeradmin3浏览0评论

At the moment I am stuck with a problem that just seems stupid, but I don't know the answer to it.

I am trying to access this JSON-object:

 var custom_fields = 
 {
   "28246": 5123,5124,5125
 }

I would like to get each value from that key. I would know how to access it if it was a nested-object, but it isn't sadly (it is ing from an API, which I can't change the JSON-response from sadly)

What I tried already is the following:

for (var key in custom_fields) {
   if (custom_fields.hasOwnProperty(key)) {
      console.log(key + " -> " + custom_fields[key]);
    }
}

The problem here is that the result will be like this:

1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> ,
6 -> 5
...etc...

Any suggestions are wele, I am trying to access it in javascript/Jquery.

Thanks for helping in advance!

At the moment I am stuck with a problem that just seems stupid, but I don't know the answer to it.

I am trying to access this JSON-object:

 var custom_fields = 
 {
   "28246": 5123,5124,5125
 }

I would like to get each value from that key. I would know how to access it if it was a nested-object, but it isn't sadly (it is ing from an API, which I can't change the JSON-response from sadly)

What I tried already is the following:

for (var key in custom_fields) {
   if (custom_fields.hasOwnProperty(key)) {
      console.log(key + " -> " + custom_fields[key]);
    }
}

The problem here is that the result will be like this:

1 -> 5
2 -> 1
3 -> 2
4 -> 3
5 -> ,
6 -> 5
...etc...

Any suggestions are wele, I am trying to access it in javascript/Jquery.

Thanks for helping in advance!

Share edited Oct 27, 2017 at 12:01 ramon abacherli asked Oct 27, 2017 at 11:54 ramon abacherliramon abacherli 2093 silver badges12 bronze badges 5
  • 1 It's just not a correct json – Faly Commented Oct 27, 2017 at 12:00
  • @Faly I thought so too, but there has to be an alternative way, for example concatenating the results separated by the ma's. Would you know how? – ramon abacherli Commented Oct 27, 2017 at 12:03
  • If the part 5123,5124,5125 is a string like "5123,5124,5125", we can do something – Faly Commented Oct 27, 2017 at 12:04
  • Is your variable 'custom_fields' an accurate example of the JSON received from the API? The three integers should be an array. – Seb Commented Oct 27, 2017 at 12:14
  • @Faly yes it was, little mistake there sorry. Thanks for helping anyways – ramon abacherli Commented Oct 27, 2017 at 12:16
Add a ment  | 

3 Answers 3

Reset to default 3

I assume that the data is in this format (note the string literals):

var custom_fields = { 
   "28246": "5123,5124,5125" 
}

If that is the case, you can use String.split. In your case, it would be something like this:

const values = custom_fields['28246'].split(',');

The values of they key 28246 are now stored in the new variable values as an array:

['5123','5124','5125']

If you want to parse all values to integers, I suggest using Array.map:

const valuesAsInt = custom_fields['28246'].split(',').map(value => parseInt(value);

Which will lead to this:

[5123, 5124, 5125]

Disclaimer: When using newer ECMAScript features such as Array.map, be sure to either use a browser which supports this our include a polyfill.

You can access it by using split function which will convert it into an array and then get the values from that array as below code.

var data = {
  "28246": '5123,5124,5125'
}
var arr = data['28246'].split(',');
$.each(arr, function( index, value ) {
  console.log(value);
});

You can split by ',' and transform each element to integer by using array.map and '+' operator:

 var custom_fields = 
 {
   "28246": "5123,5124,5125"
 }

custom_fields["28246"] = custom_fields["28246"].split(',').map(el => +el);
console.log(custom_fields);
console.log(custom_fields["28246"][0], custom_fields["28246"][1], custom_fields["28246"][2]);

发布评论

评论列表(0)

  1. 暂无评论