Calling JSON object with number name? [duplicate] - javascript

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

Related

Array fill strange result [duplicate]

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" }, {}]

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

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

Filter JSON based on some values in javascript [duplicate]

This question already has answers here:
Filter an array based on an object property [duplicate]
(2 answers)
Closed 4 years ago.
I want to filter my JSON in such a way that I only want to have data with "category": 'two'.
Data:
var json = [
{"id": 1,
"category":'one';
}
{"id": 1,
"category":'two';
}
{"id": 1,
"category":'two';
}
{"id": 1,
"category":'three';
}
]
The data is not valid. Objects inside an array and each key-value pair should be separated by comma (,). Correct that then use filter():
var json = [{"id": 1,
"category":'one'
},{"id": 1,
"category":'two'
},{"id": 1,
"category":'two'
},{"id": 1,
"category":'three'
}
]
var res = json.filter(i => i.category == 'two');
console.log(res);

How can I get a value in a nested object quickly? The object is uncertain [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 5 years ago.
I get a data from backend, the data is JSON format and nested,like
[{
a: {
b: {
c: 'value',
...
},
...
}
},....]
or maybe
[{
a: {
b: {
// no key 'c'
...
},
...
}
},....]
may also be
[{
a: {
// no key 'b'
...
}
},....]
I have to get 'a.b.c' safely, is there any good way to do it?
'a.b.c.d'?
You can use hasOwnProperty() like this
if(a.hasOwnProperty('b')){
console.log(a.b);
}
Read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Categories

Resources