Loop through an array inside an object javascript [duplicate] - javascript

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)
})

Related

Creating a copy of an array with specific index removed [duplicate]

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?

Array is not changed after filter on javascript [duplicate]

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);

Convert an object into array javascript [duplicate]

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);

Can I create a custom function and call it on an array i JavaScript [duplicate]

This question already has answers here:
adding custom functions into Array.prototype
(7 answers)
Closed 3 years ago.
How can I create a functions within JavaScript, that i can then use and chain with others to get results from an array.
I have tried creating a class with multiple methods that can be chain together but that wont allow me to call the functions directly on the array itself.
Example:
[23,45,87,89,21,234,1,2,6,7].CustomFuncEvenNumbers().CustomFuncOrderNumbers()
You can create the custom function using prototype but to chain another function you need to explicitly return from your custom function , else it will return undefined
let arr = [23, 45, 87, 89, 21, 234, 1, 2, 6, 7];
Array.prototype.CustomFuncEvenNumbers = function() {
return this.filter(function(item) {
return item % 2 === 0
})
}
let k = arr.CustomFuncEvenNumbers().map(item => item * 2);
console.log(k)

Iterate over an array and get the transformed array as a result [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Return array value with forEach() [duplicate]
(1 answer)
Closed 5 years ago.
So there are a lot of questions about iterating over an array, but I found none that says how to get the transformed array back as the left side variable. I can always do a standard for loop with indicies but I was wondering if I could use something like a .foreach that would return a transformed array.
Psedo example: I have an array points which are made up of an object Phaser.Point
Such that I can write the following code
x = new Phaser.Polygon(points.foreach(function (point) {
return new Phaser.Point(point.x+5, point.y+5)
});
new Phaser.Polygon takes an array of Phaser.Point objects
In this case, you may want to use Array.prototype.map(). Here is an example from MDN:
var numbers = [1, 5, 10, 15];
var roots = numbers.map(function(x) {
return x * 2;
});
// roots is now [2, 10, 20, 30]
// numbers is still [1, 5, 10, 15]
In your case:
x = new Phaser.Polygon(points.map(function (point) {
return new Phaser.Point(point.x+5, point.y+5)
});
References:
Array.prototype.map()
You can use Array.map. Array.map returns new array.

Categories