1

I am creating an array in Javascript where the product id is used for the key. As the key is numeric, the array is filling gaps with null.

So for example, if I had only two products and their ids were 5 and 7, I would do something like:

var arr = []
arr[5] = 'my first product';
arr[7] = 'my second product';

This array is then passed to a PHP script but upon printing the array, I get the following;

Array (
    [0] = null
    [1] = null
    [2] = null
    [3] = null
    [4] = null
    [5] = My first product
    [6] = null
    [7] = My second product
)

my ID numbers are actually 6 digits long, so when looping over the array, there are 100,000 iterations, even if I actually only have two products.

How can I create the array so the null values are not entered? I thought of making the key a string instead but as the array is build dynamically, I am not sure how to do that.

var arr = [];
for(var i=0; i<products.length; i++)
{
    array[products[i].id] = products[i].name;
}

Thanks

5
  • use a map, where id is the key Commented Oct 17, 2017 at 10:02
  • is the question about iterating a sparse array, or how to store the data without using an array? Commented Oct 17, 2017 at 10:04
  • 2
    Would an object not be a better solution? var arr = {}; arr[5] = 'my first product'; arr[7] = 'my second product'; Commented Oct 17, 2017 at 10:05
  • 1
    @Craicerjack. An object would indeed be better. The best answers are often the simplest. Thanks for your help. Commented Oct 17, 2017 at 10:09
  • var arr = []; arr.push([id, name]) ; you can access id with arr[i][0] and name with arr[i][1] . Commented Oct 17, 2017 at 10:13

3 Answers 3

1

For iterating the array, you could use Array#forEach, which skips sparse items.

var array = [];

array[5] = 'my first product';
array[7] = 'my second product';

array.forEach(function (a, i) {
    console.log(i, a);
});

For better organisation, you could use an object, with direct access with the given id.

{ 
    5: 'my first product',
    7: 'my second product'
}
Sign up to request clarification or add additional context in comments.

Comments

0

Forcing javascript key integer fills array with null

You are declaring an empty array and then setting values into 6th and 8th element in the array. That leaves the values of other elements as null.

If you don't intend to push items into array, i.e. use objects. Something like,

var obj = {};
obj[5] = "my first product";
obj[7] = "my second product";

This means the object created is:

obj = {"5":"my first product","7":"my second product"}

Comments

0

var arr = []
arr[5] = 'my first product';
arr[7] = 'my second product';
let result = arr.filter(ar => ar  != null);
console.log(result);

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.