Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array with this data in it:

[
  {
    user1post2: {
        ad:"Car", ac:39500, af:"4", ah:"klgjoirt3904d", ab:"Acura", 
        ae:"2013  ACURA MDX  3.5L V6 AWD 6AT (294 HP)", ag:"Mint", aa:"Option2"
      },
    user1post1: {
      ad:"Truck", ac:6799, af:"3", ah:"ldkfldfjljKey", ab:"GMC", 
      ae:"1/2 Ton with plow", ag:"Mint Looks", aa:"Option1"
    }
  }
]

I need to strip out user1post2 and user1post1.

I've tried using something like this, but it doesn't work:

  var valu2 = values; //put current array into new variable `valu2`

  console.log(valu2.toSource()); //Log to console to verify variable
  var finalData = [];  //Create new array
  for (var name2 in valu2) {
    finalData.push(valu2[name2]);
  };

  console.log(finalData.toSource());

How do I strip out those key values user1post2: ?

When I check the length,

console.log("length is:" + values.length);

it indicates a length of 1, so if the count starts at zero, then that would be correct. I could set up a for loop that iterates from 0 to i.

I'm not sure what the syntax or property is to reference data inside the array.

share|improve this question
2  
You're along the right lines, but values is an Array of Objects, not an Object itself. You'll need to loop over that array to remove the nesting. –  joews Mar 8 '14 at 7:56
2  
If count starts at 0 and length is 1, there is only one element in the array, element 0. I think you want to change it to var valu2 = values[0]; to get the single element which is an object with the user1post2 and user1post1 properties –  Jason Goemaat Mar 8 '14 at 8:27

2 Answers 2

up vote 3 down vote accepted

You have an array with a single object within. The keys of that object are what you want as an array. So:

var val = [{ /*... your data ...*/ }];
var out = [];
var myBigObject = val[0];

for (var i in myBigObject) {
    if (myBigObject.hasOwnProperty(i)) {
        out.push(myBigObject[i]);
    }
}

console.log(out);

http://jsfiddle.net/5xuLJ/

share|improve this answer

Is this what you want?

values = [{
    user1post2: {
        ad: "Car",
        ac: 39500,
        af: "4",
        ah: "klgjoirt3904d",
        ab: "Acura",
        ae: "2013  ACURA MDX  3.5L V6 AWD 6AT (294 HP)",
        ag: "Mint",
        aa: "Option2"
    },
    user1post1: {
        ad: "Truck",
        ac: 6799,
        af: "3",
        ah: "ldkfldfjljKey",
        ab: "GMC",
        ae: "1/2 Ton with plow",
        ag: "Mint Looks",
        aa: "Option1"
    }
}]

out = [];
for(prop in values[0]) {
    out.push(prop);
}

console.log(out);

or are you trying to iterate over the actual data inside each property (i.e. loop over user1post2 to get the value ad: "Car" etc)?

share|improve this answer
    
I tried your solution, and it gave me ["user1post2", "user1post1"] instead of everything else. –  Sandy Good Mar 8 '14 at 8:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.