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);
Related
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?
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:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of string to array of object using es6 or lodash
(4 answers)
Closed 1 year ago.
I have an array of objects as shown below
Array(2)
0: "104635ac-4b9a-3f7a-d4f8-e8badca83d58"
1: "547d57b7-ddc7-a481-95e4-ccfe2a6404a8"
I want to add two more properties one named productId and one name quantity to each element of this array of Objects.
The final outcome should be as follows.
{
"productId": "104635ac-4b9a-3f7a-d4f8-e8badca83d58",
"quantity": 1,
},
{
"productId": "547d57b7-ddc7-a481-95e4-ccfe2a6404a8",
"quantity": 1,
}
Can someone please let me know how to achieve this?
You can use Array.map to map through the array and construct an object:
const arr = ["104635ac-4b9a-3f7a-d4f8-e8badca83d58","547d57b7-ddc7-a481-95e4-ccfe2a6404a8"]
const result = arr.map(e => ({quantity: 1, productId: e}))
console.log(result)
This question already has answers here:
Convert list of Objects when the levels are given in an array
(2 answers)
Populate nested object from array?
(2 answers)
Closed 3 years ago.
What's the best way to create a N levels nested object (where N is the size of the array) for example:
const arr = ['a','b','c','d']
The output object should look like this:
{
a: {
b: {
c: {
d: true
}
}
}
}
You can use array.reduce, it helps you pass an accumulator where you can accumulate your nested obj.
const array = ['a','b','c','d'];
const object = {};
array.reduce((o, s) => {
return o[s] = {};
}, object);
console.log(object);
This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences
Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...
Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;