0

For example I have an associative array

08.02: (0) []
09.02: (1) [301]
10.02: (3) [null, null, null]
11.02: (3) [839, 695, 410]
12.02: (3) [null, null, null]
13.02: (3) [839, 679, 407]
14.02: (3) [null, null, null]
15.02: (3) [null, null, null]
16.02: (2) [403, null]
17.02: (3) [840, 690, 410]

So I want to push null in arrays , in which length is not 3. For example before push in to Array I have :

09.02: (1) [301]

But after push I want to have:

09.02: (3) [301, null, null]

EDIT: Thank you for help, I forgot about while loop.

1
  • Do we need make all the arrays with same size. i.,e if there is one element also we need to make the array length to 3 by inserting nulls ? Commented Mar 10, 2020 at 16:27

4 Answers 4

1

Just push until it's 3 long.

var arr = [301];
while(arr.length<3){
    arr.push(null);
}
console.log(arr); // Array(3) [ 301, null, null ]
Sign up to request clarification or add additional context in comments.

Comments

1

There are a few ways to do this, here's one way

const obj = {
    08.02: [],
    09.02: [301],
    10.02: [null, null, null],
    11.02: [839, 695, 410],
    12.02: [null, null, null],
    13.02: [839, 679, 407],
    14.02: [null, null, null],
    15.02: [null, null, null],
    16.02: [403, null],
    17.02: [840, 690, 410]
}

for (let key in obj) {
   while (obj[key].length < 3) {
     obj[key].push(null)
   }
}

Comments

1

One way of doing this is to have a function like this:

const fillUpToN = <A>(as: A[], n: number = 3): A[] =>
  as.length < n ? as.concat(Array(n - as.length).fill(null)) : as;

const a = [1, 2]
const b = [1]
fillUpToN(a) // [1, 2, null]
fillUpToN(b) // [1, null, null]

This looks at the argument's length to determine whether we should concatenate anything at the end, and leaves it untouched if it's already long enough.

A default parameter can be used to generalise the function even more.

fillUpToN(b, 5) // [1, null, null, null, null]

Comments

1

First of all, JavaScript does not have associative arrays. So perhaps what you're using is an object. If that's the case, then what you could do is the following:

const obj = {
  "08.02": [],
  "09.02": [301],
  "10.02": [null, null, null],
  "11.02": [839, 695, 410],
  "12.02": [null, null, null],
  "13.02": [839, 679, 407],
  "14.02": [null, null, null],
  "15.02": [null, null, null],
  "16.02": [403, null],
  "17.02": [840, 690, 410]
};

const result = Object.keys(obj).map(key => {
  const arr = obj[key];
  if (arr.length === 3) {
    return { key, items: arr };
  }
  const nulls = Array.from({ length: 3 - arr.length }).map(() => null);

  return {
    key,
    items: [...arr, ...nulls]
  };
}).reduce((accumulator, item) => {
  return {
    ...accumulator,
    [item.key]: item.items
  }
}, {});

console.log(result)

Alternatively, if you just have an array with sub arrays in it, you could do something like the following:

const arr = [
  [],
  [301],
  [null, null, null],
  [839, 695, 410],
  [null, null, null],
  [839, 679, 407],
  [null, null, null],
  [null, null, null],
  [403, null],
  [840, 690, 410]
];

const result = arr.map(subArray => {
  if (subArray.length === 3) {
    return subArray;
  }
  const nulls = Array.from({ length: 3 - subArray.length }).map(() => null);

  return [...subArray, ...nulls];
});

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.