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
Related
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 10 months ago.
Improve this question
i am dealing with strings in vuejs. Now I have 4 url strings:
https://web-sand.com/product/slug/apple-iphone-13
https://web-sand.com/product/slug/samsung-galaxy
https://web-sand.com/product/slug/xiaomi-red
https://web-sand.com/product/slug/apple-ipad
Now I want to process to get the final string. Since my string is not fixed length using fixed ways is not efficient.
Result I want to get :
apple-iphone-13
samsung-galaxy
xiaomi-red
apple-ipad
Everyone please give me any comments, thanks.
You can use:
function getStr(str) {
return str.split('\/').pop()
}
Here:
input.split("\n").map(line => line.split("/").pop())
[
'https://web-sand.com/product/slug/apple-iphone-13',
'https://web-sand.com/product/slug/samsung-galaxy',
'https://web-sand.com/product/slug/xiaomi-red',
'https://web-sand.com/product/slug/apple-ipad'
].map(item => item.split('/').pop())
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 2 years ago.
Improve this question
Im working on a Auth flow. A private project for me and some friends
My code is getting raw from A website then its put it into a vatiable.
An example of what is inside the variable
{
"token": "mytoken",
"id": "testid"
}
I would like to get the value of token and print it
let raw = {
"token": "mytoken",
"id": "testid"
}
Then raw.token or raw["token"] should do the trick.
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
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
});
});
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.