0

How can i get each variable into my javascript variable of a Javascript Array into my variable ??

I have this data

var Data = [
    {open:100,high:104.06},
    {open:103,high:105.06},
    {open:107,high:106.06},
    {open:109,high:107.06}
];

I have a function where i want to return each single value of this For example

for(var i = 0; i<Data.length; i++)
   var date = Data [i].open;

return date ;
3
  • Accept your recent questions. Commented Apr 10, 2011 at 13:22
  • I am not quite sure what do you want to achive... Commented Apr 10, 2011 at 13:23
  • _.pluck(Data, "open") Commented Apr 10, 2011 at 13:50

2 Answers 2

1

Function can't return more than one value.

What you can do, is return new array with all the "open" values:

function GetDates() {
   var dates = [];
   for(var i = 0; i<Data.length; i++)
      dates.push(Data[i].open);
   return dates;
}

This way you can get the value of the second item (103) with such code:

var arrDates = GetDates();
alert(arrDates[1]);
Sign up to request clarification or add additional context in comments.

Comments

0

Try with:

var Data = [
    {open:100,high:104.06},
    {open:103,high:105.06},
    {open:107,high:106.06},
    {open:109,high:107.06}
];

function getOpens(Data) {

    var opens = [];
    for(var i = 0; i<Data.length; i++) {
       if ( opens.indexOf( Data[i].open ) == -1 ) {
           opens.push( Data[i].open );
       }
    }

    return opens;
}

var numbers = getOpens( Data ).join(' ');

It will returns an array with open properties.

3 Comments

Thank you very much , i have tried this aproach , but each time it is returning all values , i want to return single value (FOr example each time 100,103,107,109 100,103,107,109 100,103,107,109 100,103,107,109) but i want to return only 100 103 107 109 only once
var date = Data[n].open; return date; How can i supply the value n here from the for loop ?? for(var i = 0; i<Data.length; i++) { }
I've edited answer. It will return an array of unique values. That array you can join with space separator with join(' ').

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.