1

i have tried with filter method but it removes all values. i'm getting this undefined value because of this function. I want to merge these three arrays and remove only undefined.

Example Arrays values:

let arry1= ["txt", "txt2"]; 
let arry2= ["txt"]; 
let arry3= ["txt", "txt5", "txt6"]
let c =  arry1.map(function(value, index) {
        return `${value} ${arry2[index]} ${arry3[index]}` ;
      });
c =  ['txt txt txt', 'txt2 undefined undefined']

i have this

let a = ["undefined this", "that undefined ", "undefined value undefined"]

// i want this

let a = ["this", "that", "value"]
```
5
  • 1
    "I want to merge these three arrays": Is the actual expected output the following array: ["txt txt txt", "txt2 txt5", "txt6"]? Please update your question to provide the expected output. The term "merge" is unclear as written. Commented Aug 23, 2022 at 17:56
  • 1
    You're reading the index from arry1 and applying it to arry2, which is why you're getting undefined in the first place. Commented Aug 23, 2022 at 18:02
  • What would be the desired output for the first example? Where does the array a come from? Commented Aug 23, 2022 at 18:02
  • @jsejcksn question has been updated Commented Aug 23, 2022 at 18:04
  • arr.map(str => str.replaceAll('undefined', '').trim())? Commented Aug 23, 2022 at 18:08

3 Answers 3

2

Map through the array and remove all occurrences of undefined, then trim the result:

let a = ["undefined this", "that undefined ", "undefined value undefined"]

const result = a.map(e => e.replaceAll("undefined", "").trim())
console.log(result)

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

1 Comment

This won't work if one of the actual values is "undefined" (it doesn't discriminate between missing values and the literal string "undefined").
2

you can write it like this

  let a=arry1.map(function(value, index) {
    value=`${value} ${arry2[index]||""}`.trim();
    return `${value} ${arry3[index]||""}`.trim() ;
  });

1 Comment

updated the answer this will help you to get rid of extra spaces
2

The other answers don't discriminate between the lack of a value (undefined) at an array element vs having the literal string "undefined" at an array element.

I've explained how to accommodate for that using comments in the code below:

const array1 = ["txt", "txt2"];
const array2 = ["txt"];
const array3 = ["txt", "txt5", "txt6"];

// Collect the arrays into another array so that we can iterate over them:
const arrays = [array1, array2, array3];

// Create an array of the lengths of the arrays:
const arrayLengths = arrays.map(({length}) => length);

// Get the maximum value from the lengths:
const maxLength = Math.max(...arrayLengths);

const result = [];

// One outer loop for each index of the longest array:
for (let i = 0; i < maxLength; i += 1) {
  // A placeholder string:
  let str = '';

  // One inner loop for each of the arrays:
  for (const array of arrays) {
    // The value at the current index of the current array:
    const strOrUndefined = array[i];

    // If it's actually a string, then append a space and the string
    // to the placeholder string:
    if (typeof strOrUndefined === 'string') str += ` ${strOrUndefined}`;
  }

  // Push the placeholder string into the result array,
  // but first remove the extra space created by the first iteration:
  result.push(str.trimStart());
}

console.log(result); // ["txt txt txt", "txt2 txt5", "txt6"]

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.