2

I have a javascript array (from a yahoo pipe via JSONP) in which I have a sub-array called 'moby'.

I'd like to change the current structure:

value {
  callback => blah,
  generator => blah,
  items {
    0 {
      author => blah,
      category => blah,
      moby {
        day_no => 168,
        more => Keep_this_stuff
    },
    1 {
      author => blah,
      category => blah,
      moby {
        day_no => 167,
        more => Keep_this_stuff
    },... etc
  }
}

Into a more sparse object that looks something like this:

moby {
  168 {
    day_no => 168,
    more => Keep_this_stuff
  },
  167 {
    day_no => 167,
    more => Keep_this_stuff
  },... etc
}

I know how I'd do it in ruby (with the funky Array.collect) but I have no idea in Javascript! Any clues? (I have jQuery loaded in the page I'll be using this on)

1 Answer 1

2

jQuery's map function could be used to transform an array of items into another array by using a translation function:

var result = $.map(value.items, function(element, index) {
    return element.moby;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Good use of jQuery map, but this doesn't return exactly what he wants. Still, it's a good enough clue, so here's an upvote.

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.