0

Let's say I have this Array of objects:

[300: {a:"some", b:"value"}, 301: {a: "another", b: "val"} ...]

I have that kind of object and I am iterating using recursive function, and there's no way I will change my counter to start at 300, I always start at 0.

I am thinking of a solution, to remove the keys from the agrray so that I will have an array like so:

[{a:"some", b:"value"}, {a: "another", b: "val"} ...]

How do I do that in javascript? Also, if there is another way that would be much faster than creatng a function that will remove keys, it will be much better.

5
  • 3
    You know that what you have is just an Array, with attributes 300, 301, etc, right? Commented Apr 5, 2014 at 14:45
  • ahh, yes, it'll be much better to use the word Array, thank you Commented Apr 5, 2014 at 14:47
  • Actually your expected output is an Array of objects, but your actual Array has no elements at all. Commented Apr 5, 2014 at 14:48
  • I think you misunderstand, @JoeySalacHipolito. That first thing is an object with a bunch of key/values, not an array. You've just used the wrong syntax. It should be { 300: {}... }. Try plugging that into the console and you'll get a syntax error. Commented Apr 5, 2014 at 14:50
  • uhmm, but still I used that format in looping, so I can do: somefunction(data[counter].a, data[counter].b) Commented Apr 5, 2014 at 14:52

3 Answers 3

1

This will give you a syntax error (SyntaxError: missing ] after element list):

var data = [300: {a:"some", b:"value"}, 301: {a: "another", b: "val"}];

You mean this:

var data = {300: {a:"some", b:"value"}, 301: {a: "another", b: "val"}};

And to convert that into an array, loop over the object and push each value into a new array:

var arr = [];
for (var k in data) {
  arr.push(data[k]);
}

Fiddle

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

1 Comment

actually when I looked at the retured object of my code, yeah, it's not in the [], it's in {}, dunno why I thought it was in []
1

If you meant that the initial structure of array is this:

var data = [{300: {a:"some", b:"value"}}, {301: {a: "another", b: "val"}}];

then this should work (result array):

var result = [];
var data = [{300: {a:"some", b:"value"}}, {301: {a: "another", b: "val"}}];
for(var i =0; i < data.length; i++){
    for(var key in data[i]) {
        if(data[i].hasOwnProperty(key)) {
            result.push(data[i][key]);
            break;
        }
    }
}

Comments

1

There is a point here that should be clarified. When you have a Array object which its values starts from 300 as its indexes, it means you have 300 indexes with undefined values, but if it is just an Array-like object it could be totally different.

Let's say this is not an Array-like and is an actual Array, so you have to filter it so that all the undefined values get removed from your array, so your array should be like:

var arr = [/*0*/undefined, ..,/*299*/undefined, /*300*/{a:"some", b:"value"}, /*301*/ {a: "another", b: "val"} ...]
var result = arr.filter(function(value,index){
    return (value !== undefined);
});

but what you have mentioned in your question, is more likely a javascript object, so to do what you want you can do:

var myObj = {300: {a:"some", b:"value"}, 301: {a: "another", b: "val"} };
var result = {};
for(var key in obj){
    if(obj.hasOwnProperty(key)){
        result.push(obj[key]);
    }
}

in this for loop hasOwnProperty function helps you make sure if it is one of the actual object values, and not other possible keys from the object's prototype chain.

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.