This question already has answers here:
Why doesn't splicing an object from an array in Javascript return the array?
(4 answers)
How to Clone an Array With a Removed Element? [duplicate]
(2 answers)
Closed 21 days ago.
Why does the new array only contain the value '394', instead of all the other ones?
const values = [5, 11, 394, 2, 576];
function pureSplice(givenArray: number[]){
const newArray: number[] = givenArray.splice(2, 1).map(x => x);
return newArray;
}
pureSplice(values);
The 'newArray' only contains the value '394', why does it do that and is there a way to get the array to have all values other than 394' with .splice and .map?
Related
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Filter Array modifying the original array itself [duplicate]
(3 answers)
Closed 3 months ago.
I'm trying yo filter the numeric values of an array with this code:
function getNumerics(toFilter) {
toFilter = toFilter.filter( element => !isNaN(element));
console.log(toFilter);
}
var toFilter = [1, 'z', '4', 2, 6];
getNumerics(toFilter);
console.log(toFilter);
The console.log inside the function shows a correct result but but the last console.log show the array with all the values but if I pass the array to the function why is not change? in javascript all parameters are passed are reference , aren't it?
Your function should return the filtered Array:
function getNumerics(toFilter) {
return toFilter.filter( element => !isNaN(element));
}
var toFilter = [1, 'z', '4', 2, 6];
toFilter = getNumerics(toFilter);
console.log(toFilter);
This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
(21 answers)
Closed 10 months ago.
I have an object with these key-value pairs. This object comes from an API which I am calling :
APIName(obj).subscribe((res:any) => {
console.log(Object.values(res.data));
})
data = {
0 : 1,
1: 3,
2: 7,
3: 10
....so on
}
simply put it is an object of numbers and my desired output is (a simple array) :
data = [1,3,7,10]
I've tried Object.value and Object.key it still converts it into an object. Any help?
First of all if your data is not sorted by key. Then sort it. and iterate through object and push to array.
var data = {
1: 1,
0: 3,
2: 7,
3: 10
};
let sortedKeys = Object.keys(data).sort();
let arrayOfData = [];
// iterate method
sortedKeys.forEach((key) => {
arrayOfData.push(data[key]);
});
console.log(arrayOfData);
This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
How to serialize a Map in javascript?
(3 answers)
Closed last year.
Why does this map print empty? I'd expect it to be populated from the array? I'm probably missing something small.
let numbers = [1, 2, 3];
let numberMap = numbers.reduce((map, obj) => {
return map.set(obj, obj + 1);
}, new Map());
console.log(JSON.stringify(numberMap));
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed last year.
I have this javascript object children with multiple array inside and the array names are random.
how can i loop through it without specifying 'S801', 'S901' etc so i can get some result like
[52.4655012, 13.2595648], [21.1006158, 79.0074763] , ......
Use Object.values to get the values in the Object and then map over it.
const obj = {
children: {
S801: [52, 13],
S901: [21, 79]
}
};
Object.values(obj.children).map(([value1,value2]) => {
console.log(value1,value2)
})
This question already has answers here:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Javascript string array to object [duplicate]
(4 answers)
Closed 1 year ago.
How to convert array to array object,like
['in','gs'] to [{"state":"in"},{"state":"gs"}]
map over it and return a new array
let arr = ['in','gs']
let newArr = arr.map(item => ({state: item}));
console.log(newArr);