在json数组中回复具有相同id的对象(javascript)

时间:2021-08-07 22:04:34

I would like to retrive objects with the same id in my json array made with javascript

我想在使用javascript制作的json数组中检索具有相同id的对象

var Array = {               
    "1": {"text": "Number one", "time": "16 03 13"},
    "1": {"text": "Number two", "time": "14 03 13"},
    "1": {"text": "Number three", "time": "13 03 13"},
    "2": {"text": "Number one", "time": "13 03 13"},
};

I tried in this way, but obviously it shows me only the first result (Number one)

我试过这种方式,但显然它只显示了我的第一个结果(第一)

var get = Array[1]; 
alert(get.text);

I thought about a loop, What can I do?

我想到了一个循环,我该怎么办?

1 个解决方案

#1


8  

  • Do not use Array as a variable name.
  • 不要将Array用作变量名。
  • Object keys cannot be duplicate. If you do, only the latest entry is fed to that key. The others are discarded. In your case, only the third "1" is accessible via the "1" key.
  • 对象键不能重复。如果这样做,只有最新的条目被输入该密钥。其他人被丢弃了。在您的情况下,只能通过“1”键访问第三个“1”。
  • What you need is an array, not an object
  • 你需要的是一个数组,而不是一个对象

You need this:

你需要这个:

var myArray = [               
    {"id" : 1, "text": "Number one", "time": "16 03 13"},    //index 0
    {"id" : 1, "text": "Number two", "time": "14 03 13"},    //index 1
    {"id" : 1, "text": "Number three", "time": "13 03 13"},  //index 2
    {"id" : 2, "text": "Number one", "time": "13 03 13"}     //index 3
];

var i;

for(i=0;i<myArray.length;i++){
  if(myArray[i].id === 1){
    console.log(myArray[i].text);
  }
}

Here's another approach, and this requires restructuring your data. If your entry with id of 1 has multiple values, you could hold an array under each key.

这是另一种方法,这需要重组您的数据。如果id为1的条目具有多个值,则可以在每个键下保留一个数组。

var myArray = {               
  "1" : [
    {"text": "Number one", "time": "16 03 13"},  //index 0
    {"text": "Number two", "time": "14 03 13"},  //index 1
    {"text": "Number three", "time": "13 03 13"} //index 2
  ],
  "2" : [
    {"text": "Number one", "time": "13 03 13"}   //index 0
  ]
};

var ones = myArray['1'];

for(i=0;i<ones.length;i++){
  console.log(ones[i].text);
}

Another approach is the use of Array.prototype.filter

另一种方法是使用Array.prototype.filter

var myArray = [               
  {"id" : 1, "text": "Number one", "time": "16 03 13"},    //index 0
  {"id" : 1, "text": "Number two", "time": "14 03 13"},    //index 1
  {"id" : 1, "text": "Number three", "time": "13 03 13"},  //index 2
  {"id" : 2, "text": "Number one", "time": "13 03 13"}     //index 3
];

//create an array containing only those with id of 1
var filtered = myArray.filter(function(val,i,arr){
  return val.id === 1;
});

for(i=0;i<filtered.length;i++){
  console.log(filtered[i].text);
}

#1


8  

  • Do not use Array as a variable name.
  • 不要将Array用作变量名。
  • Object keys cannot be duplicate. If you do, only the latest entry is fed to that key. The others are discarded. In your case, only the third "1" is accessible via the "1" key.
  • 对象键不能重复。如果这样做,只有最新的条目被输入该密钥。其他人被丢弃了。在您的情况下,只能通过“1”键访问第三个“1”。
  • What you need is an array, not an object
  • 你需要的是一个数组,而不是一个对象

You need this:

你需要这个:

var myArray = [               
    {"id" : 1, "text": "Number one", "time": "16 03 13"},    //index 0
    {"id" : 1, "text": "Number two", "time": "14 03 13"},    //index 1
    {"id" : 1, "text": "Number three", "time": "13 03 13"},  //index 2
    {"id" : 2, "text": "Number one", "time": "13 03 13"}     //index 3
];

var i;

for(i=0;i<myArray.length;i++){
  if(myArray[i].id === 1){
    console.log(myArray[i].text);
  }
}

Here's another approach, and this requires restructuring your data. If your entry with id of 1 has multiple values, you could hold an array under each key.

这是另一种方法,这需要重组您的数据。如果id为1的条目具有多个值,则可以在每个键下保留一个数组。

var myArray = {               
  "1" : [
    {"text": "Number one", "time": "16 03 13"},  //index 0
    {"text": "Number two", "time": "14 03 13"},  //index 1
    {"text": "Number three", "time": "13 03 13"} //index 2
  ],
  "2" : [
    {"text": "Number one", "time": "13 03 13"}   //index 0
  ]
};

var ones = myArray['1'];

for(i=0;i<ones.length;i++){
  console.log(ones[i].text);
}

Another approach is the use of Array.prototype.filter

另一种方法是使用Array.prototype.filter

var myArray = [               
  {"id" : 1, "text": "Number one", "time": "16 03 13"},    //index 0
  {"id" : 1, "text": "Number two", "time": "14 03 13"},    //index 1
  {"id" : 1, "text": "Number three", "time": "13 03 13"},  //index 2
  {"id" : 2, "text": "Number one", "time": "13 03 13"}     //index 3
];

//create an array containing only those with id of 1
var filtered = myArray.filter(function(val,i,arr){
  return val.id === 1;
});

for(i=0;i<filtered.length;i++){
  console.log(filtered[i].text);
}