Array fill strange result [duplicate] - javascript

This question already has answers here:
How to add same elements to javascript array n times
(4 answers)
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
Closed 8 months ago.
I don't get why this code:
let arr = Array(3).fill({}); // [{}, {}, {}]
arr[1].hi = "hi";
console.log(arr);
Why logs this:
[{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
Instead this:
[{}, { hi: "hi" }, {}]

Related

Object.keys() reversed when keys are 'string int' (like '8','4','2' ...) : why? [duplicate]

This question already has answers here:
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Does JavaScript guarantee object property order?
(13 answers)
Closed 2 years ago.
This is from MDN
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
OK
but why
const object1 = {
"8": 'somestring',
"4": 42,
"2": false
};
console.log(Object.keys(object1));
// expected output: Array ["2", "4", "8"]
Why we don't have Array ["8","4","2"] ?
Many thanks in advance

How to transform an indexed array into an associative array? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Convert array of objects into array of properties [duplicate]
(4 answers)
Closed 2 years ago.
Is there a way to transform the following array of objects:
array = [
{ date: "08/18", id: 1 },
{ date: "08/14", id: 2 },
{ date: "08/15", id: 3 }
]
Into this? Only returning the dates:
array2 = ["08/18", "08/14", "08/15"]
I've been trying to push the date elements as it follows, but it doesn's seem to work:
array.map(e => {
array2.push(e.date)
})
Thanks in advance.
You can use the array map function:
array2 = array.map(object => object.date);

JavaScript destructuring object containing fields with special characters [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 7 months ago.
I have array (API response):
let arr = [
{ '#type': 'Something', data: 1234 },
{ '#type': 'Something', data: 3214 },
]
Is it possible to destructure elements with those '#' prefixed fields?
for (const { data, ??? #type } of arr) {}
You could take a computed property and a new variable name.
let arr = [{ '#type': 'Something', data: 1234 }, { '#type': 'Something', data: 3214 }];
for (const { data, ['#type']: renamed } of arr) {
console.log(renamed);
}

Convert a Javascript array to a JSON object [duplicate]

This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
How do I take a javascript array
var array = [1,2,3,4,5,6]
And convert it to a JSON object that looks like this
[
{
"value": 1
},
{
"value": 2
},
{
"value": 3
},
{
"value": 4
},
{
"value": 5
},
{
"value": 6
}
]
The closest I've come is using this
JSON.stringify(Object.assign({}, array));
But it gives me an output like this
{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6}
You could map the values by taking a short hand property.
var array = [1, 2, 3, 4, 5, 6],
result = array.map(value => ({ value }));
console.log(result);

Calling JSON object with number name? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 3 years ago.
Ex:
{
"123": [
...stuff
]
}
How do I call "123" in a jquery without returning an error?
I've tried:
$.getJSON('insert-url-here').then(function (data) {
console.log(data.123.length)
}
but it doesn't work.
Use square bracket notation:
const data = {
"123": [1, 2, 3]
}
console.log(data[123].length);
Or since it's technically a string:
const data = {
"123": [1, 2, 3]
}
console.log(data["123"].length);

Categories

Resources