2

I have an array which is missing values for some indices, Can you please suggest which is the best way to fill them with either null or 0 something, I have tried possible ways in lodash and nothin seems to be working.

Followed below question and nothing helped

Filling the missing index and filling its value with 0

fill multiple missing values with series based on index values

Python - filling a list of tuples with zeros in places of missing indexes

console.log of my array yields below output

[ 5289, <1 empty item>, 20460, 10860, '189', 'D2989', <1 empty item>, <2 empty items>, '-' ]

Where I want to fill this as

[ 5289, null, 20460, 10860, '189', 'D2989', null, null,null, '-' ]

Ron
  • 1,024
  • 4
  • 24
  • 52
  • 1
    "missing values for some indices", does this mean they are "undefined"/"null"? Or are they "empty" slots? – Nick Parsons Mar 01 '20 at 06:43
  • 2
    Why did you look up Python solutions? Sparse arrays will have undefined at uninitialized indices. What's the actual issue? Without any code or context it's difficult to know what you actually want. – Dave Newton Mar 01 '20 at 06:45
  • 1
    You can't have "missing" array indexes in Javascript. Indexes always start at 0, and the maximum valid index will be determined by the length of the array. Do you want to add more elements at the end of the array, i.e. to pad its length? – kmoser Mar 01 '20 at 06:45
  • Please share a minimal example of how your array variable looks like. – palaѕн Mar 01 '20 at 06:46
  • Honestly, I am confused there as well But When I do console.log I get as `[ 5289, <1 empty item>, 20460, 10860, '189', 'KA51D2989', <1 empty item>, <2 empty items>, '-' ]` – Ron Mar 01 '20 at 06:46
  • And what do you think that means? – Dave Newton Mar 01 '20 at 06:47
  • @DaveNewton , I tried looking for solution but most of the solutions are available in Python – Ron Mar 01 '20 at 06:49
  • @DaveNewton, I have updated my question with exact Input and Output – Ron Mar 01 '20 at 06:50
  • @From where does this array come from? – Coldark Mar 01 '20 at 06:50
  • @Coldark actually this is the array generated by one of the node library – Ron Mar 01 '20 at 06:52
  • Are you relying solely on console log, in a browser, using the default array string representation? – Dave Newton Mar 01 '20 at 06:52
  • Perhaps you want something like: `Array.from(arr, v => v ?? null)`, you can change the nullish-coalescing operator to something more browser-friendly if you can't support it – Nick Parsons Mar 01 '20 at 06:53
  • Actually I am printing the values on the node server and not on browser – Ron Mar 01 '20 at 06:53
  • 1
    Have you read this? https://stackoverflow.com/questions/51688366/why-are-empty-items-in-my-array-and-how-do-i-get-rid-of-them – kmoser Mar 01 '20 at 06:54
  • @Ron Do you want to preserve existing indices and array length? – Coldark Mar 01 '20 at 07:05
  • Yes @Coldark , I want to preserve the length and existing indices also – Ron Mar 01 '20 at 07:06
  • @Coldark,@DaveNewton,@Nick Parsons thanks for your time, I solved it `test.forEach(function (o, index) { var len = test.length; for (var i = 0; i < len; i++) { if (test[i] === undefined) { test[i] = null; } } });` For sure my question deserves downvotes and I welcome that – Ron Mar 01 '20 at 07:08

3 Answers3

3

Spread operator can convert empty to undefined:

const list = new Array(5);
list[3] = "text";

console.log([...list]);
dcporter7
  • 426
  • 2
  • 11
  • First of all, `map` doesn't modify the initial array it returns a new one, and apart from that this doesn't work as `map` only iterates over entries that have indices. `undefined` entries in your array have indices - they are not the same as actual empty entries. – Coldark Mar 01 '20 at 08:11
  • @Coldark I misread. Updated my answer. – dcporter7 Mar 01 '20 at 08:18
  • That's actually really clever, I'm for sure upvoting this. – Coldark Mar 01 '20 at 08:21
1

Since you've solved your problem already:

@Coldark,@DaveNewton,@Nick Parsons thanks for your time, I solved it

test.forEach(function(o, index) {
  var len = test.length;
  for (var i = 0; i < len; i++) {
    if (test[i] === undefined) {
      test[i] = null;
    }
  }
}); 

In case someone gets here looking for a solution to similar problem I'm going to say, that you could even do it as simple as:

for (let i = 0; i < arr.length; i++) {
  if (!arr[i]) { // or arr[i] === undefined if your array may have other falsy values in it
    arr[i] = null;
  }
}

Empty entries are caused by deleting array values i.e:

for (let i = 0; i < arr.length; i++) {
    delete arr[i];
}

which generally should not be used, unless it's intentional and we're fully aware of consequences. The main difference between empty or non existing entries and any of "empty" values like null or undefined is that the latter still can be accessed by indices.

It's greatly explained here

Coldark
  • 387
  • 3
  • 13
1

I think the simplest way is using the Array.from.

const arr = new Array(4);
arr[0] = 5219;

console.log(arr)

const arr_fill = Array.from(arr, x => x || null);

console.log(arr_fill);
Siva K V
  • 7,622
  • 2
  • 11
  • 24