tengo un problema Tengo una matriz como se muestra a continuación:
0: {type: 'Text', id_a: '123789', value: 'The fifth chapter of the World Première Ducati will be held at the Expo Dubai on December 9th.', number: 2, id: 7} 1: {type: 'Image', id_a: '123789', value: 'b_desertx-dwp2022-2-uc336332-high.jpg', number: 3, id: 8} 2: {type: 'Video youtube', id_a: '123789', value: 'https://youtu.be/SnuyDoXxC4g', number: 5, id: 10} 3: {type: 'Image', id_a: '123456', value: 'moto_guzzi_v100_mandello.jpg', number: 3, id: 3} 4: {type: 'Text', id_a: '123456', value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', number: 2, id: 2}De estos quiero, por ejemplo, tomar aquellos que tienen un id_a igual a 123456 y que me devuelvan el valor (por lo tanto, refiriéndose a id_a = 123456 debe devolver los dos arreglos relativos) entonces
3: {type: 'Image', id_a: '123456', value: 'moto_guzzi_v100_mandello.jpg', number: 3, id: 3} 4: {type: 'Text', id_a: '123456', value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', number: 2, id: 2}Excepto que obtengo esto a continuación
{type: 'Image', id_a: '123456', value: 'moto_guzzi_v100_mandello.jpg', number: 3, id: 3} {type: 'Text', id_a: '123456', value: 'The star of the Piaggio group stand is without doubt ... Discover all her secrets with our video', number: 2, id: 2}sin la numeración para poder recuperar el valor (por lo tanto utilizando el atributo de valor ) de cada uno de ellos.
¿Cómo puedo hacer esto?
var myArr = JSON.parse(this.responseText); for(var i = 0, len = myArr.Items.length; i < len; i++){ var id_art = myArr.Items[i].id_a; if(id_art == 123456) { myArr.Items[i]; console.log(myArr.Items[i]); }Entonces, según la edición, la matriz con la información real que queremos es el valor de la clave Elementos.
myArr no parece ser una matriz, pero en realidad también es un par clave-valor basado en la edición.
Aquí está mi intento:
var myArr = JSON.parse(this.responseText); //console.log(myArr.Items) //Declare and initialize kv pair. var results = {}; for (var i = 0, len = myArr.Items.length; i < len; i++) { var id_art = myArr.Items[i].id_a; if (id_art == 123456) { //console.log(i, myArr.Items[i]); //Create the key-value pair and store the index information in the key. results[i] = myArr.Items[i]; } } console.log(results);Producción:
{ '3': { type: 'Image', id_a: '123456', value: 'moto_guzzi_v100_mandello.jpg', number: 3, id: 3 }, '4': { type: 'Text', id_a: '123456', value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', number: 2, id: 2 } }Explicación: Dado que ha especificado, no le importa cómo se almacenan los datos. Lo puse en la clave de un nuevo objeto clave-valor en Javascript (¿llaman a estos diccionarios o hashmaps aquí?).
Por lo tanto, puede acceder a ellos ahora recorriendo los elementos del diccionario u obteniendo una lista de solo claves en el diccionario y eligiendo a qué valor desea acceder.
Por ejemplo, para imprimir una lista de claves que existen en este objeto, puede usar:
for (let value of Object.keys(results)) { console.log(value) }Dime si te he entendido bien.
Editar
Para obtener el resultado mencionado en su comentario, ahora use while :
var x = 0; while (x < Object.entries(results).length) { //console.log(Object.entries(results)[x][1]); if (Object.entries(results)[x][1].type == "Image"){ console.log("The index is: ", Object.entries(results)[x][0]); console.log("The value is: ", Object.entries(results)[x][1].value); } x++; }