0

I want to create an array by extracting the dates of some other array and then comma separate the values into a string.

      array.push({date:created_at,username:user});
      for (i in array) {
      var combined=new array();
      combined = array[i].date;
                }   
      console.log(combined);

I am new to javascript and hard to follow in arrays.Thanks !! Can anyone also recommend me a good book for javascript?

1
  • You are re-declaring the array in each iteration of the for loop. Also, try w3schools.com . Commented Jul 27, 2012 at 13:44

3 Answers 3

1

Try this

var originalArray = [{date:"2012-01-01", username: "first"}, {date:"2012-01-02", username: "second"}];

// First step: Get a dateArray with only the dates
var dateArray = [];
for (var i in originalArray) {
    dateArray.push(originalArray[i].date);
}   

// Or if you prefer to cut a few lines
// dateArray = originalArray.map(function(x) { return x.date; } );

// Second step: Get it as a comma separated string
var datesString = dateArray.join(",");

console.log(dateArray); // ["2012-01-01","2012-01-02"]
console.log(datesString); // 2012-01-01,2012-01-02

One of the more popular books is "Javascript The Good Parts" by Douglas Crockford http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

Sign up to request clarification or add additional context in comments.

4 Comments

Join is a lot better than my rubbish. Oh well it's friday I guess.
And when we are talking about clean code and it being friday. Let's celebrate with array.map! The first step could have been: dateArray = originalArray.map(function(x) { return x.date; });
One liner (cannot really recommend it though): [{date:"2012-01-01", username: "first"}, {date:"2012-01-02", username: "second"}].map(function(a) { return a.date; }).join(",");
hahaha yeah I had the same thought. Neat, but looks terribad. Chaining map and join though isn't too bad.
0

here is the code :

var firstArray = new Array(new Date(), "hello", "something", new Date());
var combined = new Array();

for(i in firstArray) {
   if(firstArray[i] instanceof Date) {
      combined[combined.length] = firstArray[i];
   }
 }

 var theString = combined.join(",");

 alert(combined.length);
 alert(theString);

Comments

0

I personally am partial to Javascript the Good parts, but there is a whole community wiki on books for programming. As for your question if you want to use a string, you need to use a string not an array for the combined variable.

array.push({date:created_at,username:user});
var combined = array.map(function(a) { return a.date; }).join(", ");

There are certainly better ways of doing it, but this is just a quick example of something which would work. By concatenating with a string, it implicitly casts the array[i].date into a string if it wasn't already.

Edit: Fixed my code to not be so awful.

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.