I'm having a problem. I have an array as shown below:
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}
Of these I want, for example, to take those that have an id_a equal to 123456 and have me return the value (therefore, referring to id_a = 123456 it must return the two relative arrays) then
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}
Except I get this below
{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}
without the numbering in order to be able to retrieve the value (therefore using the value attribute) of each of them.
How can I do this?
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]);
}
myArr is:
{Items: Array (5), Count: 5, ScannedCount: 5}
Count: 5
Items: Array (5)
0: {type: 'Text', id_a: '123789', value: 'The fifth chapter of the Ducati World Première will take place 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 her secrets with our video', number: 2, id: 2}
length: 5
WHILE
var x=0;
while(x <= Object.keys(results).length){
if(Object.keys(results[x])=="Image")
console.log(results[x].value);
}
CodePudding user response:
So based on the edit the array with actual information we want is the value of the Items key.
myArr doesn't seem to be an array, but it's actually a key-value pair based on the edit as well.
Here is my attempt:
var myArr = JSON.parse(this.responseText);
//console.log(myArr.Items)
//Declare and initialize k-v 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);
Output:
{
'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
}
}
Explanation: Since you have specified you do not care how the data is stored. I have put it in the key of a new key-value Object in Javascript (do they call these dictionaries or hashmaps here?).
Thus, you can access them now by cycling through the elements in the dictionary, or getting a list of just keys in the dictionary and choosing which value you would like to access.
For instance, to print out a list of keys that exist in this object, you might use:
for (let value of Object.keys(results)) {
console.log(value)
}
Let me know if I have understood you correctly.
Edit
To get the output mentioned in your comment, now using 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 ;
}

