0

I've got the following array which is not ideal for looping through. If I wanted to get the title of every item in the array there's no easy way to do it because the keys are unique (don't ask me how I ended up with an array that's this all-over-the-place).

productsSelectedArray = [
  {cpu: {title: "Core i7 2.4GHz", price: 200}},
  {memory: {title: "8GB", price: 100}},
  {videoCard: {title: "nVidia 6500-GTS", price: 400}},
  {display: {title: "27-inch LCD", price: 200}},
  {extraBattery: {title: "", price: ""}},
  {antivirus: {title: "", price: ""}},
  {mouse: {title: "Logitech 232", price: 50}},
  {extendedWarranty: {title: "", price: ""}}
]

I want to be able to loop through it so that it returns

"Core i7 2.4GHz"

"8GB"

"nVidia 6500-GTS"

"27-inch LCD"

""

""

"Logitech 232"

""

I'd like to convert the array above into an array like below. It's essentially removing the first key of each hash and shifting the rest of the hash over (the new "key" being the index of the array).

newProductsSelectedArray = [
  {title: "Core i7 2.4GHz", price: 200},
  {title: "8GB", price: 100},
  {title: "nVidia 6500-GTS", price: 400},
  {title: "27-inch LCD", price: 200},
  {title: "", price: ""},
  {title: "", price: ""},
  {title: "Logitech 232", price: 50},
  {title: "", price: ""}
]

This way I can loop through it and do like newProductsSelectedArray[i].title (with a loop) to display all the titles in order, and so on.

I've been trying to find a solution for this for an hour or so now, but I'm still not sure how to do it. delete just deletes the entire key along with all the values. I don't know how to remove a key and "shift" the rest of the hash over, and I don't think there would be a function like that anyway because it's kind of a nonsensical thing to normally do.

The main problem is that the key names are all different, preventing me from looping and displaying all the titles.

I could write a loop using Object.keys(productsSelectedArray[i])[0] that makes all the key names item, and then I could loop using productsSelectedArray[i].item.title but maybe there is a more elegant solution?

3
  • you want just values of each key I think ? Commented Jul 7, 2014 at 14:38
  • Yeah, edited for clarity. Commented Jul 7, 2014 at 14:42
  • productsSelectedArray.map(function(o) { for(k in o) return o[k]}) Commented Jul 7, 2014 at 14:48

3 Answers 3

1

This should work for you:-

arrayWithTitles = productsSelectedArray.map(function (product) {
    var obj = {};
    for(key in product){
      obj = product[key];
    }
    return obj;
});
Sign up to request clarification or add additional context in comments.

7 Comments

@fuzzybabybunny I think you should have a look on array native functions. Your last question was also can be solved just like this.
yeah, agreed. I need more practice with the map function.
here is a MDN reference of array where you will get all useful array native methods, like map,filter,reduce etc.
Hmmm... I'm a bit confused. I understand that the map method goes like this: MyArray.map(functionThatIWantToRunOnEachItemOfMyArray) But where are you defining product, the parameter that you are passing into your anonymous function?
Ok, I did some console logging and figured it out. Unfortunately, the MDN docs are not very clear with their explanation of the map function, especially for newbies. product is simply each element in the array. When .map takes a function with one parameter, that parameter will always be the first element in the array, then the second element, then the third, etc as .map loops through the array's elements one by one.
|
1

I think this is what you want:

//option 1 update the existing array
for(var i=0;i<productsSelectedArray.length;i++){
  for(var x in productsSelectedArray[i]){
    productsSelectedArray[i] = productsSelectedArray[i][x];
    break;
  }
} 

//option 2 create a new array
var newProductsSelectedArray = [];
for(var i=0;i<productsSelectedArray.length;i++){
  for(var x in productsSelectedArray[i]){
    newProductsSelectedArray.push(productsSelectedArray[i][x]);
    break;
  }
} 

It loops over the main array, then over the map inside automatically setting the new value to whatever the first match is.

Here's a jsFiddle showing it in action (option 1)(open your console to see the results) http://jsfiddle.net/5gyuz/

3 Comments

Holy crap. That's clever.
Ohhh... I see. I was in the wrong line of thinking. I was thinking about shifting things over. Instead I should have been thinking of just writing over the hash itself.
@fuzzybabybunny yeah you can go either way... overwrite the existing array (if the other stuff is "junk") or push into a new array.
0

You should look into Array.forEach, which will solve your underlying problem.

You don't need to iterate using indices, you can simply use a function that acts on each element of the array.

A basic example:

productsSelectedArray.forEach(function(element, index) {
    console.log("Title at index " + index + " is " + element.title;
});

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.