Array of objects length [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a file which have the following data. And i am assigning it to arr.
[
{
"data": {
"One": {
"One one": [
"Hello"
]
}
},
"name": "1898061c-bd49-4e35-bfb6-6514fbe9c5c4"
}
]
When i access the first element like console.log(arr.length) it gives me 1 in chrome console (manually declaring array there). which is fine.
But when i console log to actual terminal it gives me 209.

Make sure that the "arr" object is an array and not a string.
The array has length one, the stringified JSON of the array has length 209:
JSON.stringify(arr, null, 4).length; // => 209

Related

searching a string in array by partial of it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I wanna find a string inside an array by some part of its name(at least more than 3characters), Like:
Buffalo is an index of array and I want to search buf or buff, buffa, buffal or even the hole word(buffalo) and it return Buffalo.
No matter if its case-sensitive or NOT
here is my array:
const carsName =
[
"Landstalker",
"Bravura",
"Buffalo",
"Linerunner",
"Perrenial",
"Sentinel",
"Dumper",
"Firetruck",
"Trashmaster",
"Stretch",
"Manana",
"Infernus"
]
I wrote it in PAWN once and I'm gonna write it in JS (for my Alt V server) but I'm stuck at this point, is it possible to do?
and at the end, sorry for my English
Case insensitive partial match:
const carsName = [
"Landstalker",
"Bravura",
"Buffalo",
"Linerunner",
"Perrenial",
"Sentinel",
"Dumper",
"Firetruck",
"Trashmaster",
"Stretch",
"Manana",
"Infernus",
];
const partial = 'buf';
const foundName = carsName.find(
name => name.toLowerCase().includes(partial.toLowerCase())
);
console.log(foundName);

JS: Why does [] + "abc" result in an array of 3 elements of letters? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm confused on the behavior of this expression in JavaScript:
[] + "abc"
I am expecting it to simply produce an array with element 0 being undefined and element 1 being "abc":
[, "abc"]
but the result is:
["a", "b", "c"]
This seems perhaps non-intuitive. Why is the behavior?
A string is returned because [] is casted as "".
`console.log([].toString());// ""
const a = [] + "abc";
console.log(a);// "abc"

Access values from JSON in NodeJS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a function that will take in a JSON object which has a similar structure as follows:-
{
"listing": [
{
"rental_prices": {
"shared_occupancy": "N",
"per_week": 2308,
"accurate": "per_month",
"per_month": 10000
},
"country_code": "gb",
"num_floors": "0",
}
]
}
I want to be able to get all the values for per_month from each sub object of listing I can pull a specific one, i.e. using data.listing[0].rental_prices.per_month but I want to be able to iterate over the object and get the value and push it to a new array each time it comes aross the per_month key
Indeed as #mwilson suggested .map would do the trick in this case.
https://repl.it/repls/TriangularModestNormalform
testJson.listing.map(listing => listing.rental_prices.per_month);
Note: This will return an array with undefined items if per_month doesn't exist, and will also crash if renal_prices doesn't exist

How to parse or loop this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

Javascript array of object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to have an array containing an object. For example I have an object like this:
parentObj:{
childObj1,
childObj2
}
but I want to have a "parentObj" array and be able to call it like :
parentObj[0].childObj1
I searched a lot but didn't find anything related to this. I'll appreciate if someone can help me with this.
thanks very much
Here's an array of objects:
var a = [
{name: "first"},
{name: "second"}
];
console.log(a[0].name); // "first"
console.log(a[1].name); // "second"
The [] is an array initializer. The {} is an object initializer.

Categories