I receive a data
value from a API, and I want to make a condition to deal with it. Sometimes it can es as an array or object. I will use a simple example.
data = [1,2,3] // These values e from API
data.map(i => i++)
The problem is: Sometimes data
can also es as this
data = {
arr: [1,2,3]
}
// It evals an error in .map, because now "data" is an object
I know that I can solve it making something like this:
if(Array.isArray(data))
data.map(i => i++);
else
data.arr.map(i => i++);
But my code is not just a one line .map
. Is there a way to make this simple condition without copying and paste code?
Thanks!
I receive a data
value from a API, and I want to make a condition to deal with it. Sometimes it can es as an array or object. I will use a simple example.
data = [1,2,3] // These values e from API
data.map(i => i++)
The problem is: Sometimes data
can also es as this
data = {
arr: [1,2,3]
}
// It evals an error in .map, because now "data" is an object
I know that I can solve it making something like this:
if(Array.isArray(data))
data.map(i => i++);
else
data.arr.map(i => i++);
But my code is not just a one line .map
. Is there a way to make this simple condition without copying and paste code?
Thanks!
Share Improve this question asked Dec 11, 2018 at 11:39 JohnsonJohnson 1,5466 gold badges21 silver badges36 bronze badges 2-
1
Easiest and maintainable way would be to have a helper function
function getData( data ) { return Array.isArray( data ) ? data : data.arr }
and then use that everywheregetData( data ).map( i => i++ )
– Ivan Drinchev Commented Dec 11, 2018 at 11:41 - @drinchev you should put your solution as an answer, I think your solution is better for me :) – Johnson Commented Dec 11, 2018 at 12:02
6 Answers
Reset to default 3You can for example assign the array reference to another variable and use it in the rest of your code, like this:
let arr = Array.isArray(data) ? data : data.arr;
arr.map(i => i++)
A simple OR (||
) operator is pretty idiomatic JavaScript:
(data.arr || data).map(i => ++i);
If the data.arr
property is defined, that will be mapped, otherwise data
itself will be mapped.
Complete snippet:
Note: the post-increment operator would have no effect, so I replaced it with a pre-increment.
let data, result;
data = [1, 2, 3];
result = (data.arr || data).map(i => ++i);
console.log(result);
data = {
arr: [1, 2, 3]
}
result = (data.arr || data).map(i => ++i);
console.log(result);
You can use the ternary operator.
Array.isArray(data)
? data.map(i => i++);
: data.arr.map(i => i++);
You can use destruction es6 , not sure its a good idea but you can achieve your functionality in single line. ;)
let { arr=data } = data;
arr.map(i => i++)
if arr key is not found in data then it will assign default data array. Cheers
You can do it like this
You can use the ternary operator and assign the value as array directly to temp if it is an Array and if not than you assign using the property like input.arr
which is an Array.
So once the value is in form of array than you can use the single map statement so you need not to repeat your map statement.
let data = [1,2,3];
let data1 = {
arr: [1,2,3]
}
function handle(input){
let temp = Array.isArray(input) ? input : input.arr
return temp.map(i => i++);
}
console.log(handle(data))
console.log(handle(data1))
If you don't wish to use an if
or a ternary
operator you can use Object.values(data).flat()
to convert your data
into:
[1, 2, 3]
This will essentially not modify your array and leave it be, however, it will press your data object into an array form.
See working examples below:
Data form 1 (obj):
const data = {arr: [1, 2, 3]};
const res = Object.values(data).flat().map(i => ++i);
console.log(res);
Data form 2 (array):
const data = [1, 2, 3];
const res = Object.values(data).flat().map(i => ++i);
console.log(res);
Do note, however, Object.values
does not guarantee order, and thus your array may lose its order. Moreover, if you plan to use this in production .flat()
isn't yet supported across all browsers and instead, you may consider looking at a polyfill option