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 24 days ago.
Improve this question
I'm trying restructure an array and build another one. Tried to use filter, map, but couldn't achieved that. There are some factors in array and it's ratings (the 5 elements of object from the beginning). I should collect them in another array. I have an array like this.
[{
"carryRating": 21,
"distanceRating": 72,
"obHavoYogingarchilikQorVaHKRating": 74,
"obHavoQuruqRating": 40,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 32,
"id": 1,
"direction": "A",
"distance": "90.55",
"carry": "365",
"obHavoQuruq": "50",
"yetkazibBeruvchiningMoliyaviyAhvoli": "56",
"obHavoYogingarchilikQorVaHK": "58"
}, {
"carryRating": 35,
"distanceRating": 74,
"obHavoYogingarchilikQorVaHKRating": 71,
"obHavoQuruqRating": 29,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 55,
"id": 2,
"direction": "B",
"distance": "82.46",
"carry": "589",
"obHavoQuruq": "36",
"yetkazibBeruvchiningMoliyaviyAhvoli": "98",
"obHavoYogingarchilikQorVaHK": "65"
}, {
"carryRating": 19,
"distanceRating": 76,
"obHavoYogingarchilikQorVaHKRating": 90,
"obHavoQuruqRating": 17,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 11,
"id": 3,
"direction": "C",
"distance": "76.16",
"carry": "326",
"obHavoQuruq": "21",
"yetkazibBeruvchiningMoliyaviyAhvoli": "20",
"obHavoYogingarchilikQorVaHK": "23"
}, {
"carryRating": 25,
"distanceRating": 78,
"obHavoYogingarchilikQorVaHKRating": 65,
"obHavoQuruqRating": 14,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 2,
"id": 4,
"direction": "D",
"distance": "72.11",
"carry": "421",
"obHavoQuruq": "17",
"yetkazibBeruvchiningMoliyaviyAhvoli": "3",
"obHavoYogingarchilikQorVaHK": "78"
}]
I want to restructure it like this:
const newData = [
{
factor: "carry",
firstElementOfArray: 21, // it's carryRating
secondElementOfArray: 35,
thirdElementOfArray: 19,
fourthElementOfArray: 25
},
{
factor: "distance",
firstElementOfArray: 72, // it's distanceRating
secondElementOfArray: 74,
thirdElementOfArray: 76,
fourthElementOfArray: 78
},
{
factor: "obHavoQuruq",
firstElementOfArray: 40, // it's obHavoQuruqRating
secondElementOfArray: 29,
thirdElementOfArray: 17,
fourthElementOfArray: 14
},
{
factor: "yetkazibBeruvchiningMoliyaviyAhvoli",
firstElementOfArray: 32, // it's yetkazibBeruvchiningMoliyaviyAhvoliRating
secondElementOfArray: 55,
thirdElementOfArray: 11,
fourthElementOfArray: 2
},
{
factor: "obHavoYogingarchilikQorVaHK",
firstElementOfArray: 74, // it's obHavoYogingarchilikQorVaHKRating
secondElementOfArray: 71,
thirdElementOfArray: 90,
fourthElementOfArray: 65
},
]
How can I do that?
I'd solve it by mapping keys with properties, and iterating over them.
This solution does require you to have a fixed number of items in the input array, but it could be expanded with some kind of "translation" from [x] to 'x-thElementOfArray' if needed as well.
let input = [{
"carryRating": 21,
"distanceRating": 72,
"obHavoYogingarchilikQorVaHKRating": 74,
"obHavoQuruqRating": 40,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 32,
"id": 1,
"direction": "A",
"distance": "90.55",
"carry": "365",
"obHavoQuruq": "50",
"yetkazibBeruvchiningMoliyaviyAhvoli": "56",
"obHavoYogingarchilikQorVaHK": "58"
}, {
"carryRating": 35,
"distanceRating": 74,
"obHavoYogingarchilikQorVaHKRating": 71,
"obHavoQuruqRating": 29,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 55,
"id": 2,
"direction": "B",
"distance": "82.46",
"carry": "589",
"obHavoQuruq": "36",
"yetkazibBeruvchiningMoliyaviyAhvoli": "98",
"obHavoYogingarchilikQorVaHK": "65"
}, {
"carryRating": 19,
"distanceRating": 76,
"obHavoYogingarchilikQorVaHKRating": 90,
"obHavoQuruqRating": 17,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 11,
"id": 3,
"direction": "C",
"distance": "76.16",
"carry": "326",
"obHavoQuruq": "21",
"yetkazibBeruvchiningMoliyaviyAhvoli": "20",
"obHavoYogingarchilikQorVaHK": "23"
}, {
"carryRating": 25,
"distanceRating": 78,
"obHavoYogingarchilikQorVaHKRating": 65,
"obHavoQuruqRating": 14,
"yetkazibBeruvchiningMoliyaviyAhvoliRating": 2,
"id": 4,
"direction": "D",
"distance": "72.11",
"carry": "421",
"obHavoQuruq": "17",
"yetkazibBeruvchiningMoliyaviyAhvoli": "3",
"obHavoYogingarchilikQorVaHK": "78"
}];
let propertyMapping = {
carry: "carryRating",
distance: "distanceRating",
obHavoQuruq: "obHavoQuruqRating",
yetkazibBeruvchiningMoliyaviyAhvoli: "yetkazibBeruvchiningMoliyaviyAhvoliRating",
obHavoYogingarchilikQorVaHK: "obHavoYogingarchilikQorVaHKRating"
};
let output = Object.keys(propertyMapping).map(key => ({
factor: key,
firstElementOfArray: input[0][propertyMapping[key]],
secondElementOfArray: input[1][propertyMapping[key]],
thirdElementOfArray: input[2][propertyMapping[key]],
fourthElementOfArray: input[3][propertyMapping[key]]
}));
console.log(output);
Update
Alternatively, as you added that the source data will not always contain 4 items, it would be better to use a different output format (as proposed in the comments as well).
For example:
let output = Object.keys(propertyMapping).map(key => ({
factor: key,
items: input.map(it => it[propertyMapping[key]])
}));
or
let output = Object.keys(propertyMapping).reduce((prev, key) => ({ ...prev,
[key]: input.map(it => it[propertyMapping[key]])
}), {})
I want to create a recursive function that receive a List of objects that contains the id and parent_id. If the parent of an element is in the list I want to remove it and add it to the parent.
Convert this:
{
"id": 180,
"children": [],
"parent_id": 195,
"name": "Object 180"
},
{
"id": 193,
"children": [],
"parent_id": 180,
"name": "Object 193"
},
{
"id": 194,
"children": [],
"parent_id": 180,
"name": "Object 194"
}
{
"id": 199,
"children": [],
"parent_id": 187,
"name": "Object 199"
}
{
"id": 304,
"children": [],
"parent_id": 193,
"name": "Object 304"
}
To this:
{
"id": 180,
"children": [
{
"id": 193,
"children": [
{
"id": 304,
"children": [],
"parent_id": 193,
"name": "Object 304"
}
],
"parent_id": 180,
"name": "Object 193"
},
{
"id": 194,
"children": [],
"parent_id": 180,
"name": "Object 194"
}
],
"parent_id": 195,
"name": "Object 180"
},
{
"id": 199,
"children": [],
"parent_id": 187,
"name": "Object 199"
}
Sometimes the parent_id is null, and there is no limit of levels of the parents.
Since basarat's answer does not account for items nested more than one level.
Here a solution that creates an output with arbitrary nesting-depth:
const listToTree = (input) => {
const map = new Map(input.map((item) => [item.id, item]));
const output = [];
for (const item of input) {
if (map.has(item.parent_id)) {
map.get(item.parent_id).children.push(map.get(item.id));
} else {
output.push(map.get(item.id));
}
}
return output;
};
const input = [
{
"id": 180,
"value": 10,
"children": [],
"parent_id": 195,
"name": "Object 180"
},
{
"id": 193,
"value": 10,
"children": [],
"parent_id": 180,
"name": "Object 193"
},
{
"id": 194,
"value": 10,
"children": [],
"parent_id": 180,
"name": "Object 194"
},
{
"id": 199,
"children": [],
"parent_id": 187,
"name": "Object 199"
},
{
"id": 304,
"value": 10,
"children": [],
"parent_id": 193,
"name": "Object 304"
},
{
"id": 305,
"value": 10,
"children": [],
"parent_id": 194,
"name": "Object 304"
}
];
const output = listToTree(input);
console.log(output);
Edit: Aggregate values
If you want to aggregate values along ancestry chains of the resulting tree I would recommend to do this in a separate function afterwards. This will keep your code cleaner, easier to test and more readable.
The implementation depends on whether or not your input-array is sorted (children before parents). If you want to process unordered inputs you have to loop through the each ancestry chain.
function aggregateValue(branch) {
const children = branch.children || [];
return children.reduce((sum, child) => sum + aggregateValue(child), branch.value || 0);
}
function aggregateValueAlongBranches(tree) {
return tree.map((branch) => {
return {
...branch,
aggregatedValue: aggregateValue(branch),
children: aggregateValueAlongBranches(branch.children),
};
});
}
const input = [
{
"id": 180,
"value": 10,
"children": [
{
"id": 193,
"value": 10,
"children": [
{
"id": 304,
"value": 10,
"children": [],
"parent_id": 193,
"name": "Object 304"
}
],
"parent_id": 180,
"name": "Object 193"
},
{
"id": 194,
"value": 10,
"children": [
{
"id": 305,
"value": 10,
"children": [],
"parent_id": 194,
"name": "Object 304"
}
],
"parent_id": 180,
"name": "Object 194"
}
],
"parent_id": 195,
"name": "Object 180"
},
{
"id": 199,
"children": [],
"parent_id": 187,
"name": "Object 199"
}
];
const output = aggregateValueAlongBranches(input);
console.log(output);
You don't need a recursive function. Just track items you've already seen and if parent exists in it add to parent.children or add a new root node.
An example complete solution is attached.
Complete code
type Item = {
id: number,
children: Item[],
parent_id: number,
name: string,
}
const items: Item[] = [
{
"id": 180,
"children": [],
"parent_id": 195,
"name": "Object 180"
},
{
"id": 193,
"children": [],
"parent_id": 180,
"name": "Object 193"
},
{
"id": 194,
"children": [],
"parent_id": 180,
"name": "Object 194"
},
{
"id": 199,
"children": [],
"parent_id": 187,
"name": "Object 199"
},
{
"id": 304,
"children": [],
"parent_id": 193,
"name": "Object 304"
}
];
function nest(items:Item[]): Item[] {
const output: Item[] = [];
const idToItem = new Map<number,Item>();
for (let item of items) {
// Either add to parent. Or create a new root level node
if (idToItem.has(item.parent_id)) {
idToItem.get(item.parent_id)!.children.push(item);
} else {
idToItem.set(item.id, item);
output.push(item);
}
}
return output;
}
console.log(nest(items));
I am creating a little React application about Pokemons. I have in DB informations about all of them (about 900+).
They all contain an id field, which is an integer from 1 to 900+.
But the problem is when I do a request like this :
firebase.database().ref(`mydb`).orderByChild('id').startAt(1).limitToFirst(limit).once('value')
The results are not correct: I have an id array like this: [9,1, 10, 6, 4]
Am I doing something wrong ?
Edit:
I add the result I got when I perform the request I wrote above, I added a custom_id field containing ids as strings, but I stille have an unordered result:
[
{
"base_experience": 239,
"custom_id": "009",
"height": 16,
"id": 9,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/9/encounters",
"name": "blastoise",
"order": 12,
"weight": 855
},
{
"base_experience": 64,
"custom_id": "001",
"height": 7,
"id": 1,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/1/encounters",
"name": "bulbasaur",
"order": 1,
"weight": 69
},
{
"base_experience": 39,
"custom_id": "010",
"height": 3,
"id": 10,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/10/encounters",
"name": "caterpie",
"order": 14,
"weight": 29
},
{
"base_experience": 240,
"custom_id": "006",
"height": 17,
"id": 6,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/6/encounters",
"name": "charizard",
"order": 7,
"weight": 905
},
{
"base_experience": 62,
"custom_id": "004",
"height": 6,
"id": 4,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/4/encounters",
"name": "charmander",
"order": 5,
"weight": 85
},
{
"base_experience": 142,
"custom_id": "005",
"height": 11,
"id": 5,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/5/encounters",
"name": "charmeleon",
"order": 6,
"weight": 190
},
{
"base_experience": 142,
"custom_id": "002",
"height": 10,
"id": 2,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/2/encounters",
"name": "ivysaur",
"order": 2,
"weight": 130
},
{
"base_experience": 63,
"custom_id": "007",
"height": 5,
"id": 7,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/7/encounters",
"name": "squirtle",
"order": 10,
"weight": 90
},
{
"base_experience": 236,
"custom_id": "003",
"height": 20,
"id": 3,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/3/encounters",
"name": "venusaur",
"order": 3,
"weight": 1000
},
{
"base_experience": 142,
"custom_id": "008",
"height": 10,
"id": 8,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/8/encounters",
"name": "wartortle",
"order": 11,
"weight": 225
}
]
You've not attached your database image, but with the order one thing is sure that these keys in your database are strings.
And when you order string data, it is ordered lexicographically.
So for numbers, this is the normal order:
1
9
10
But for strings, this is the normal order:
"9"
"1"
"10"
I don't think there is any operator in Firebase (nor in most other databases) to change this behaviour.
Instead, you will have to modify the data to get the behavior you want. So: store values that are in the order you need them when sorted lexicographically.
For numbers you can accomplish that by padding them with zeroes:
"001"
"009"
"010"
EDIT:
Now after Json, these values are stored in id field so the above thing does not apply to this.
You may however use a code like this, to do what you're trying:
mDatabase
.child("main_child")
.orderByChild("id")
.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
System.out.println(child.getKey());
}
}
...
I have a json object with some place id. Now I am trying to get the respective address for each item.
This is what I am trying:
<div id="places" class="places"></div>
<script>
function initialize() {
json_data = jQuery.parseJSON(...);
for (i = 0; i < json_data.length; i++) {
console.log(json_data[i].id);
console.log(json_data[i].key);
var request = {
placeId: json_data[i].key
};
service = new google.maps.places.PlacesService(document.getElementById("places"));
service.getDetails(request, callback);
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
$(".places").append("<p>" + request.placeId + " - " + place.formatted_address + "</p>");
}
}
}
}
However the output is not what I am expecting. The console.log is ok, but the append only shows some data from the API. DEMO
It seems like the api takes some time to process. Not sure how to handle that.
You try to execute bunch of place details requests on the client side JavaScript code. The most important thing you should be aware of is the existence of client side rate quota. It was mentioned in the previous version of documentation, however, I cannot find it in the new Google Maps Platform documentation. Not sure if Google will remove this limit after July 16, 2018 after official launch of new pricing model.
In old documentation it was mentioned this way:
Service requests are rate-limited per user session, regardless of how many users share the same project. When you first load the service API, you are allocated an initial quota of requests. Once you use this quota, the API enforces rate limits on additional requests on a per-second basis. If too many requests are made within a certain time period, the API returns an OVER_QUERY_LIMIT response code. The per-session rate limit prevents the use of client-side services for batch requests. For batch requests, use the Maps API web services.
Typically per-session quota allowed a bucket of 10 requests, once bucket is empty you could execute only 1 request per second. So you have to check the state of the response and in case of OVER_QUERY_LIMIT repeat request after 1 second delay.
I modified your code in order to solve your issue
function initialize() {
var json_data = jQuery.parseJSON('[ { "id": 146, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 145, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 144, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 143, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 142, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 141, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 140, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 139, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 138, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 137, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 136, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 135, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 134, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 133, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 132, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 131, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 130, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 129, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 128, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 127, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 126, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 125, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 124, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 123, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 122, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 121, "key": "ChIJ2QhxhMHOHg0RF8zBGoV9sVQ" }, { "id": 120, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 119, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 118, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 117, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 116, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 115, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 114, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 113, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 112, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 111, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 110, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 109, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 108, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 107, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 106, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 105, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 104, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 103, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 102, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 101, "key": "ChIJEYNl2y3JHg0R9zyZw3xCJ6A" }, { "id": 100, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 99, "key": "ChIJ9QQaFsHOHg0ROav_i9VD3V4" }, { "id": 98, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 97, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 96, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 95, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 94, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 93, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 92, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 91, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 90, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 89, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 88, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 87, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 86, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 85, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 84, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 83, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 82, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 81, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 80, "key": "ChIJSfWKOnvMHg0RqLLi-22LWnI" }, { "id": 79, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 77, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 76, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 75, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 74, "key": "ChIJ4zGTOfhmJA0RfmcqcLKlVKk" }, { "id": 73, "key": "ChIJQRdO0HrMHg0RThqjF9M9-4w" }, { "id": 72, "key": "ChIJbdGsShT5Ig0RuaZCe0dkRWM" }, { "id": 71, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 70, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 69, "key": "ChIJr367qitvJA0Rvy5Oh88YCUE" }, { "id": 68, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 67, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 66, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 65, "key": "ChIJK8leGat-GA0R6uUjP8gMizY" }, { "id": 64, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 63, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 62, "key": "ChIJzVzD6z42Iw0RmkTnIYjAv_Y" }, { "id": 61, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 60, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 59, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 58, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 57, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 56, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 55, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 54, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 53, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 52, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 51, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 50, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 49, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 48, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 47, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 46, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 45, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 44, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 43, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 42, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 41, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 40, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 39, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 38, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 37, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 36, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 35, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 34, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 33, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 32, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 31, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 30, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 29, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 28, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 27, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 26, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 25, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 24, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 23, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 22, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 21, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 20, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 19, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 18, "key": "ChIJVT4alwcuGQ0RoHvco1jDCQ0" }, { "id": 17, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 16, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 15, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 14, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 13, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 12, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 11, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 10, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 9, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 8, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 7, "key": "ChIJyxnnULr4Gg0R9zGpUZLEL1M" }, { "id": 6, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 5, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 4, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 3, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 2, "key": "ChIJS2ITKK8zGQ0R4ECQ5L3rAAU" }, { "id": 1, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" } ]');
var delayFactor = 0;
var service = new google.maps.places.PlacesService(document.getElementById("places"));
function m_get_places (request) {
service.getDetails(request, function(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
$(".places").append("<p>" + request.placeId + " - " + place.formatted_address + "</p>");
} else if (status === google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
delayFactor++;
setTimeout(function () {
m_get_places(request);
}, delayFactor * 1000);
} else {
console.log(status);
}
});
}
for (i = 0; i < json_data.length; i++) {
console.log( json_data[i].id );
console.log( json_data[i].key );
var request = {
placeId: json_data[i].key
};
m_get_places(request);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initialize"></script>
<div id="places" class="places"></div>
You can find this example on jsfiddle as well: https://jsfiddle.net/xomena/kvq07xyr/
I hope this helps!
So i have this json
"inventory": {
"data": {
"0": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 0,
"name": "Slot Extender (Low)",
"desc": null
},
"4": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 4,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"7": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 7,
"name": "Slot Extender (Low)",
"desc": null
},
"8": {
"id": 1454,
"upgrade": 0,
"bind": 0,
"slot": 8,
"name": "Vampiric Earring +7",
"desc": null
},
"11": {
"id": 12,
"upgrade": 0,
"bind": 0,
"slot": 11,
"name": "Return Stone",
"desc": null
},
"12": {
"id": 2425,
"upgrade": 0,
"bind": 0,
"slot": 12,
"name": "Vital Potion (Lv. 2)",
"desc": null
},
"14": {
"id": 3094,
"upgrade": 0,
"bind": 0,
"slot": 14,
"name": "Holy Water of Critical Strike (30 min)",
"desc": null
},
"16": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 16,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"20": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 20,
"name": "Upgrade Core (Medium)",
"desc": null
},
"22": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 22,
"name": "Upgrade Core (Medium)",
"desc": null
},
"23": {
"id": 2444,
"upgrade": 0,
"bind": 0,
"slot": 23,
"name": "Strike Potion (Lv. 1)",
"desc": null
},
"24": {
"id": 19,
"upgrade": 9,
"bind": 4,
"slot": 24,
"name": "Citrine Orb",
"desc": null
},
"26": {
"id": 124,
"upgrade": 15,
"bind": 1,
"slot": 26,
"name": "Aramid Battlesuit",
"desc": null
},
"28": {
"id": 2704,
"upgrade": 0,
"bind": 0,
"slot": 28,
"name": "Weapon Option Scroll (High)",
"desc": null
},
"29": {
"id": 214,
"upgrade": 15,
"bind": 4,
"slot": 29,
"name": "Aramid Battleboots",
"desc": null
},
"44": {
"id": 169,
"upgrade": 13,
"bind": 4,
"slot": 44,
"name": "Aramid Battlegloves",
"desc": null
},
"47": {
"id": 663,
"upgrade": 0,
"bind": 0,
"slot": 47,
"name": "Force Regen. Potion (Lv. 1)",
"desc": null
},
"48": {
"id": 1,
"upgrade": 0,
"bind": 0,
"slot": 48,
"name": "Upgrade Core (High)",
"desc": null
},
"56": {
"id": 2342,
"upgrade": 0,
"bind": 0,
"slot": 56,
"name": "Holy Water of Fighter",
"desc": null
},
"59": {
"id": 2381,
"upgrade": 0,
"bind": 0,
"slot": 59,
"name": "Enchant Safeguard (Highest)",
"desc": null
},
"64": {
"id": 2338,
"upgrade": 0,
"bind": 0,
"slot": 64,
"name": "Holy Water of Vitality",
"desc": null
},
"65": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 65,
"name": "Snow Ice MP Potion",
"desc": null
},
"66": {
"id": 5,
"upgrade": 0,
"bind": 0,
"slot": 66,
"name": "HP Potion (Lv. 3)",
"desc": null
},
"68": {
"id": 1381,
"upgrade": 0,
"bind": 0,
"slot": 68,
"name": "Copper Coin",
"desc": null
},
"72": {
"id": 320,
"upgrade": 0,
"bind": 0,
"slot": 72,
"name": "Life Absorb Ring +2",
"desc": null
},
"74": {
"id": 582,
"upgrade": 0,
"bind": 0,
"slot": 74,
"name": "Force Core (Low)",
"desc": null
},
"75": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 75,
"name": "Force Core (High)",
"desc": null
},
"76": {
"id": 2339,
"upgrade": 0,
"bind": 0,
"slot": 76,
"name": "Holy Water of Speed",
"desc": null
},
"80": {
"id": 3278,
"upgrade": 0,
"bind": 0,
"slot": 80,
"name": "Skill Book (Split Specialty Stage 3)",
"desc": null
},
"82": {
"id": 3276,
"upgrade": 0,
"bind": 0,
"slot": 82,
"name": "Skill Book (Sword Splitter)",
"desc": null
},
"84": {
"id": 3277,
"upgrade": 0,
"bind": 0,
"slot": 84,
"name": "Skill Book (Split Specialty Stage 2)",
"desc": null
},
"86": {
"id": 34,
"upgrade": 5,
"bind": 1,
"slot": 86,
"name": "Citrine Crystal",
"desc": null
},
"96": {
"id": 3293,
"upgrade": 0,
"bind": 0,
"slot": 96,
"name": "Minesta Training Book Chapter 15",
"desc": null
},
"105": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 105,
"name": "Upgrade Core (Highest)",
"desc": null
},
"106": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 106,
"name": "Upgrade Core (Highest)",
"desc": null
},
"107": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 107,
"name": "Upgrade Core (Highest)",
"desc": null
},
"128": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 128,
"name": "Snow Ice MP Potion",
"desc": null
},
"129": {
"id": 2345,
"upgrade": 0,
"bind": 0,
"slot": 129,
"name": "Holy Water of Flawless Defense",
"desc": null
},
"130": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 130,
"name": "Snow Ice MP Potion",
"desc": null
},
"131": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 131,
"name": "Snow Star HP Potion",
"desc": null
},
"132": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 132,
"name": "Snow Star HP Potion",
"desc": null
},
"133": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 133,
"name": "Snow Star HP Potion",
"desc": null
},
"134": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 134,
"name": "Snow Ice MP Potion",
"desc": null
},
"142": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 142,
"name": "Force Core (High)",
"desc": null
},
"196": {
"id": 1275,
"upgrade": 0,
"bind": 0,
"slot": 196,
"name": "Periodical Remote shop card",
"desc": null
}
}
This is a json object of some items into some character's inventory. The key is basically the item slot ID in that inventory.
What i want is to fill with empty arrays based on missing keys in AngularJS. For example if you take the first 2 items, first has 0 key and the second one has key 4 so there are missing 1,2,3 and i want to fill with empty arrays with those missing keys. And i want this process to repeat till the maximum of 255 arrays!
Can anyone nicely please help me with a code or something?
you can do something like this:
for( var i=0;i<255;i++){
if(inventory.data[i] == undefined){
inventory.data[i] = {};
}
}
I assume that you want to assign empty object to non existing keys.
One other way of doing this would be like ;
var data = {
"0": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 0,
"name": "Slot Extender (Low)",
"desc": null
},
"4": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 4,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"7": {
"id": 637,
"upgrade": 0,
"bind": 0,
"slot": 7,
"name": "Slot Extender (Low)",
"desc": null
},
"8": {
"id": 1454,
"upgrade": 0,
"bind": 0,
"slot": 8,
"name": "Vampiric Earring +7",
"desc": null
},
"11": {
"id": 12,
"upgrade": 0,
"bind": 0,
"slot": 11,
"name": "Return Stone",
"desc": null
},
"12": {
"id": 2425,
"upgrade": 0,
"bind": 0,
"slot": 12,
"name": "Vital Potion (Lv. 2)",
"desc": null
},
"14": {
"id": 3094,
"upgrade": 0,
"bind": 0,
"slot": 14,
"name": "Holy Water of Critical Strike (30 min)",
"desc": null
},
"16": {
"id": 3247,
"upgrade": 0,
"bind": 0,
"slot": 16,
"name": "Lamp of Dazzlement (Lv. 4)",
"desc": null
},
"20": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 20,
"name": "Upgrade Core (Medium)",
"desc": null
},
"22": {
"id": 10,
"upgrade": 0,
"bind": 0,
"slot": 22,
"name": "Upgrade Core (Medium)",
"desc": null
},
"23": {
"id": 2444,
"upgrade": 0,
"bind": 0,
"slot": 23,
"name": "Strike Potion (Lv. 1)",
"desc": null
},
"24": {
"id": 19,
"upgrade": 9,
"bind": 4,
"slot": 24,
"name": "Citrine Orb",
"desc": null
},
"26": {
"id": 124,
"upgrade": 15,
"bind": 1,
"slot": 26,
"name": "Aramid Battlesuit",
"desc": null
},
"28": {
"id": 2704,
"upgrade": 0,
"bind": 0,
"slot": 28,
"name": "Weapon Option Scroll (High)",
"desc": null
},
"29": {
"id": 214,
"upgrade": 15,
"bind": 4,
"slot": 29,
"name": "Aramid Battleboots",
"desc": null
},
"44": {
"id": 169,
"upgrade": 13,
"bind": 4,
"slot": 44,
"name": "Aramid Battlegloves",
"desc": null
},
"47": {
"id": 663,
"upgrade": 0,
"bind": 0,
"slot": 47,
"name": "Force Regen. Potion (Lv. 1)",
"desc": null
},
"48": {
"id": 1,
"upgrade": 0,
"bind": 0,
"slot": 48,
"name": "Upgrade Core (High)",
"desc": null
},
"56": {
"id": 2342,
"upgrade": 0,
"bind": 0,
"slot": 56,
"name": "Holy Water of Fighter",
"desc": null
},
"59": {
"id": 2381,
"upgrade": 0,
"bind": 0,
"slot": 59,
"name": "Enchant Safeguard (Highest)",
"desc": null
},
"64": {
"id": 2338,
"upgrade": 0,
"bind": 0,
"slot": 64,
"name": "Holy Water of Vitality",
"desc": null
},
"65": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 65,
"name": "Snow Ice MP Potion",
"desc": null
},
"66": {
"id": 5,
"upgrade": 0,
"bind": 0,
"slot": 66,
"name": "HP Potion (Lv. 3)",
"desc": null
},
"68": {
"id": 1381,
"upgrade": 0,
"bind": 0,
"slot": 68,
"name": "Copper Coin",
"desc": null
},
"72": {
"id": 320,
"upgrade": 0,
"bind": 0,
"slot": 72,
"name": "Life Absorb Ring +2",
"desc": null
},
"74": {
"id": 582,
"upgrade": 0,
"bind": 0,
"slot": 74,
"name": "Force Core (Low)",
"desc": null
},
"75": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 75,
"name": "Force Core (High)",
"desc": null
},
"76": {
"id": 2339,
"upgrade": 0,
"bind": 0,
"slot": 76,
"name": "Holy Water of Speed",
"desc": null
},
"80": {
"id": 3278,
"upgrade": 0,
"bind": 0,
"slot": 80,
"name": "Skill Book (Split Specialty Stage 3)",
"desc": null
},
"82": {
"id": 3276,
"upgrade": 0,
"bind": 0,
"slot": 82,
"name": "Skill Book (Sword Splitter)",
"desc": null
},
"84": {
"id": 3277,
"upgrade": 0,
"bind": 0,
"slot": 84,
"name": "Skill Book (Split Specialty Stage 2)",
"desc": null
},
"86": {
"id": 34,
"upgrade": 5,
"bind": 1,
"slot": 86,
"name": "Citrine Crystal",
"desc": null
},
"96": {
"id": 3293,
"upgrade": 0,
"bind": 0,
"slot": 96,
"name": "Minesta Training Book Chapter 15",
"desc": null
},
"105": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 105,
"name": "Upgrade Core (Highest)",
"desc": null
},
"106": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 106,
"name": "Upgrade Core (Highest)",
"desc": null
},
"107": {
"id": 1214,
"upgrade": 0,
"bind": 0,
"slot": 107,
"name": "Upgrade Core (Highest)",
"desc": null
},
"128": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 128,
"name": "Snow Ice MP Potion",
"desc": null
},
"129": {
"id": 2345,
"upgrade": 0,
"bind": 0,
"slot": 129,
"name": "Holy Water of Flawless Defense",
"desc": null
},
"130": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 130,
"name": "Snow Ice MP Potion",
"desc": null
},
"131": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 131,
"name": "Snow Star HP Potion",
"desc": null
},
"132": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 132,
"name": "Snow Star HP Potion",
"desc": null
},
"133": {
"id": 1115,
"upgrade": 0,
"bind": 0,
"slot": 133,
"name": "Snow Star HP Potion",
"desc": null
},
"134": {
"id": 1116,
"upgrade": 0,
"bind": 0,
"slot": 134,
"name": "Snow Ice MP Potion",
"desc": null
},
"142": {
"id": 2,
"upgrade": 0,
"bind": 0,
"slot": 142,
"name": "Force Core (High)",
"desc": null
},
"196": {
"id": 1275,
"upgrade": 0,
"bind": 0,
"slot": 196,
"name": "Periodical Remote shop card",
"desc": null
}
},
ar = Array(255).fill().map((e,i) => data[i] || {}); // all in array
console.log(ar);
obj = Array(255).fill().reduce((p,c,i) => (p[i] = data[i] || {},p),{}); // all in object
console.log(obj);