1

I'm stuck with a problem. I have a object with a array of objects inside and I want to iterate through it and in each array I get I want specific positions.

So I want for the first element to get the [0], the second I want [0], the third I want [2] and the last element I want [0].

values ************** [ anonymous { medium: [ 0.28, 0.26, 0.13 ] },
4|wscontro |   anonymous { medium: [ 13.51, 0.04, 0.75 ] },
4|wscontro |   anonymous { medium: [ 1527.58, 262.98, 27.4, 59.49 ] },
4|wscontro |   anonymous { medium: [ 60305.25, 0 ] } ]
4|wscontro | values type of -------- object
4|wscontro | values ************** [{"medium":[0.28,0.26,0.13]},{"medium":[13.51,0.04,0.75]},{"medium":[1527.58,262.98,27.4,59.49]},{"medium":[60305.25,0]}]
4|wscontro | item ---->>>  anonymous { medium: [ 0.28, 0.26, 0.13 ] }
4|wscontro | item ---->>>  anonymous { medium: [ 13.51, 0.04, 0.75 ] }
4|wscontro | item ---->>>  anonymous { medium: [ 1527.58, 262.98, 27.4, 59.49 ] }
4|wscontro | item ---->>>  anonymous { medium: [ 60305.25, 0 ] }

And I want to get

0.28, 13.51, 27.4, 60305.25

I dont know how to get the index of the key with my forEach.

my code:

var allPromises=[];                 

                allPromises.push(Database.Alerts.getDeviceSystemByDeviceId("system.load", device.id));
                allPromises.push(Database.Alerts.getDeviceSystemByDeviceId("disk_space.sda3", device.id));
                allPromises.push(Database.Alerts.getDeviceSystemByDeviceId("system.ram", device.id)); 
                allPromises.push(Database.Alerts.getDeviceSystemByDeviceId("platform.temperatures", device.id));

                Promise.all(allPromises).then(function(values){
                    console.log("values **************", values);
                    console.log("values type of --------", typeof(values));

                    console.log("values **************", JSON.stringify(values));

                    values.forEach(function(item){
                        console.log("item ---->>> ", item);
                    });
2
  • 3
    Why do you not use a simple for loop? Commented Sep 4, 2017 at 11:41
  • The second parameter to a forEach is the index.. eg.. ["a","b","c"].forEach((a, ix) => { console.log(a, ix); }) would show a a 0, b 1, c 2 Commented Sep 4, 2017 at 11:43

2 Answers 2

1

Add an index parameter to the function called inside your forEach :

values.forEach(function(item, index){
    console.log("item ---->>> ", item);
    console.log("index --->>> ", index);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Array.prototype.forEach as well as Array.prototype.map can actually give you the index as a second parameter into the callback. As you want to get back several numbers (perhaps in an array) then .map suits your needs pretty well.

const relevantValues = values.map(function(item, index){
      if(index == 2) { return item.medium[2] }
      else { return item.medium.[0] }
 });

That would make relevantValues equal [0.28, 13.51, 27.4, 60305.25].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.