3

I have an array like:

var array = [1, 2, undefined, undefined, 7, undefined]

and need to replace all undefined values with "-". The result should be:

var resultArray = [1, 2, "-", "-", 7, "-"]

I think there is a simple solution, but I couldn't find one.

4
  • Already has an answer here Commented Apr 16, 2018 at 9:17
  • is zero a valid value? Commented Apr 16, 2018 at 9:18
  • do you have a sparse array? Commented Apr 16, 2018 at 9:20
  • yes, zero is a valid value Commented Apr 16, 2018 at 9:23

9 Answers 9

4

You could check for undefined and take '-', otherwise the value and use Array#map for getting a new array.

var array = [1, 2, undefined, undefined, 7, undefined],
    result = array.map(v => v === undefined ? '-' : v);
    
console.log(result);

For a sparse array, you need to iterate all indices and check the values.

var array = [1, 2, , , 7, ,],
    result = Array.from(array, v => v === undefined ? '-' : v);
    
console.log(result);

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

Comments

3

You can use Array.map

var array = [1, 2, undefined, undefined, 7, undefined];
var newArray = array.map(x => x !== undefined ? x : "-");
console.log(newArray);

Comments

2

You can map the values and return - if undefined.

let array = [1, 2, undefined, undefined, 7, undefined]

let result = array.map(o => o !== undefined ? o : '-');

console.log(result);

Comments

2

Use Array.map()

var array = [1, 2, undefined, undefined, 7, undefined];
console.log(array);

var newArray = array.map(function(v) {
  return undefined === v ? '-' : v;
});

console.log(newArray);

Comments

1
var newArray = array.map(function(val) {
    if (typeof val === 'undefined') {
        return '-';
    }
    return val;
});

Comments

1
 function SampleArray() {
            var Array = [];
            var array = [1, 2, undefined, undefined, 7, undefined];
            for (var i = 0; array.length > i; i++) {
                var Value;
                if (array[i] == undefined) {
                    Value = '-';
                } else {
                    Value = array[i];
                }

                Array.push(Value);
            }
        }

Comments

0

Try using this -

array.forEach(function(part,index,Arr){ if(!Arr[index])Arr[index]='-'})

Comments

0

You can use functional programming map function:

 let array = [1, 2, undefined, undefined, 7, undefined];

 let array2 = array.map(e => e === undefined ?  e='-' :  e);

1 Comment

the same answer with the done one! why -1? this is a misleading for people searching for real help!
0

Try:

while((index = array.indexOf(undefined)) > -1){array[index] = "-";}

"-"

this will search for the index of the value you are searching for, and replace it with "-"

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.