0

var arr = []; console.log(arr); console.log("Length: " + arr.length); arr[1] = [{},{}]; console.log(arr); console.log("Length: " + arr.length); arr[3] = [{},{}]; console.log(arr); console.log("Length: " + arr.length);

So when I create an array like above it gives empty/undefined elements in between.

I want to remove those empty/undefined elements while preserving the index values.

vm.eArray = vm.eArray.filter(function (arr) { return arr.length; }); 

I'm using the above code to remove the undefined elements but it messes my index/key values.

Or is there any way to avoid it at first place?

10
  • 1
    Why are you inserting element to array[1] where there is no value in array[0]?
    – edkeveked
    CommentedNov 29, 2017 at 7:42
  • Arrays are 0 indexed I think this is being overlooked... facepalm moment for sure :)CommentedNov 29, 2017 at 7:43
  • What you are looking for is called a Map.CommentedNov 29, 2017 at 7:43
  • @edkeveked i want to store id's in index.CommentedNov 29, 2017 at 7:44
  • 1
    @edkeveked Can you give an example?CommentedNov 29, 2017 at 7:50

4 Answers 4

2

Array are an index data structure. So when using them, it is better to keep that structure otherwise there is no use to use an array. For your use case, you can use whether a Map(), or an array of Json element.

  • Using a Map, your indexes (1, 2, 3) can become directly the keys of your array.
  • Another solution could be to use an array of Json element, where each element has a key and a value

var myMap = new Map(); //to add the element with index 1 myMap.set(1, [{},{}]); //to add the element with index 3 myMap.set(3, [{},{}]); // you can iterate over them if you want like an array console.log("with a map") myMap.forEach((key, value) => console.log(key, value)); // using a Json object var myObject = []; myObject.push({id: 1, value:[{},{}]}) myObject.push({id: 3, value:[{},{}]}) //iterate over it, because it is still an array, but of Json element console.log("with array of json") for (element of myObject){ console.log(element.id, element.value) }

1
  • I have used Json object. Thanks for your efforts. Now i understand the use of map as wellCommentedNov 30, 2017 at 7:41
2

var arr = new Map(); console.log(arr); console.log("Length: " + arr.size); arr.set(1,[{},{}]); console.log(arr); console.log("Length: " + arr.size); arr.set(3,[{},{}]); console.log(arr); console.log("Length: " + arr.size);

You can use Map(), and get the size using map.size.

2
  • can i use angular.forEach or ng-repeat on map? orCommentedNov 29, 2017 at 7:48
  • I was on that same page. Thanks anywaysCommentedNov 29, 2017 at 7:52
1

I would just use an object to store your values.

 var arr = {}; console.log(arr); console.log("Length: " + Object.keys(arr).length); arr[1] = [{},{}]; console.log(arr); console.log("Length: " + Object.keys(arr).length); arr[3] = [{},{}]; console.log(arr); console.log("Length: " + Object.keys(arr).length); for (something in arr){ console.log(arr[something]); } 
    0

    You are assigning value arr[1] = arr[1] = [{},{}]; at index 1. So you are skipping index 0. By default, javascript will place undefined there.

    0

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.