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);
Related
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));
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 need help in merging the values of the objects in an array if the id is the same as the warehouse_item_id. In my case, there is a two objects which should be merge id: 191 and id: 52, because id 52 has warehouse_item_id of 191.
Please see the response and the code below.
CODE
const yeah = yes.filter(a => a.id === a.warehouse_item_id);
JSON Response
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
EXPECTED OUTPUT
[
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
},
{
"id": 52,
"outlet_item_id": null,
"warehouse_item_id": 191,
"ingredient_id": 65,
"quantity": 7,
"total_in_lowest": 0,
"stock_on_hand": 0,
"adjustment_price": 0,
"soh_total_in_lowest": 0,
"unit_price": 50,
"difference": 0,
"difference_in_lowest": 0
}
]
> EXPECTED OUTPUT
yes = [
{
"id": 191,
"warehouse_id": 24,
"ingredient_id": 65,
"expiration_date": "2019-07-31",
"available_stocks": 7,
"ingredient": {
"id": 65,
"name": "erg",
"SKU": "1000064",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 50
}
},
{
"id": 192,
"warehouse_id": 24,
"ingredient_id": 66,
"expiration_date": "2019-09-18",
"available_stocks": 33994,
"ingredient": {
"id": 66,
"name": "gvf",
"SKU": "1000065",
"default_unit": {
"id": 27,
"name": "Gram"
},
"purchase_price": 60
}
},
{
"id": 193,
"warehouse_id": 24,
"ingredient_id": 67,
"expiration_date": "2019-09-19",
"available_stocks": 43996,
"ingredient": {
"id": 67,
"name": "fwefe",
"SKU": "1000066",
"default_unit": {
"id": 26,
"name": "Milliliter"
},
"purchase_price": 70
}
}
]
Here is the steps which you need to follow.
Create a new array for merged objects
Iterate the array
Find the item which you're looking for
merge with the existing object to the filtered object
append to new object
done.
let json = [{"id":191,"warehouse_id":24,"ingredient_id":65,"expiration_date":"2019-07-31","available_stocks":7,"ingredient":{"id":65,"name":"erg","SKU":"1000064","default_unit":{"id":26,"name":"Milliliter"},"purchase_price":50}},{"id":192,"warehouse_id":24,"ingredient_id":66,"expiration_date":"2019-09-18","available_stocks":33994,"ingredient":{"id":66,"name":"gvf","SKU":"1000065","default_unit":{"id":27,"name":"Gram"},"purchase_price":60}},{"id":193,"warehouse_id":24,"ingredient_id":67,"expiration_date":"2019-09-19","available_stocks":43996,"ingredient":{"id":67,"name":"fwefe","SKU":"1000066","default_unit":{"id":26,"name":"Milliliter"},"purchase_price":70}},{"id":52,"outlet_item_id":null,"warehouse_item_id":191,"ingredient_id":65,"quantity":7,"total_in_lowest":0,"stock_on_hand":0,"adjustment_price":0,"soh_total_in_lowest":0,"unit_price":50,"difference":0,"difference_in_lowest":0}];
// new array after merging the objects
let desiredArray = [];
// iterate the array
let processedWarehouseItems = [];
for (let i = 0; i < json.length; i++) {
let newObj = json[i]; // assign the each object if no filtered found in case
// find the object based on your filter condition
let fiteredObj = json.filter(
e => e.id != json[i].id && e.warehouse_item_id === json[i].id
);
if (fiteredObj && fiteredObj.length) {
// if object found
processedWarehouseItems.push(fiteredObj[0].warehouse_item_id);
newObj = { ...json[i], ...fiteredObj[0]
}; // merge the objects and creates a new one
}
// check if the item is already merged in?
if (processedWarehouseItems.indexOf(json[i].warehouse_item_id) === -1) {
desiredArray.push(newObj); // push to the new array
}
}
console.log(desiredArray)
In Model Derivative API (OBJ Extraction using DbIds), I retrieved OBJ URN using API. As Response shows below, it returns all children's URN for obj, mtl, and its zip. How do we identify the recent extraction as every child does have its own guid?
I have attached whole response JSON. What I am looking for is the second obj file which has extracted using several dbids like "17,19,21,22,23...". The first child obj was created long ago. How do I differentiate the new obj. As GUID's are different for every node. How do I identify which GUID.obj belongs to my desired extraction.
** Edited **
I am looking for identifying extracted wavefronts object files( not objectids and name which gets from metadata). Suppose if I extracted 2 object files from one model, then after completion of object file geometry extraction job, 6 file gets generated and store in bucket.
first .obj (guid = /output/geometry/e72fa6b3-4fc2-3c86-8424-5d0755539c0d.obj)
first .mtl
first .zip
second .obj
second .mtl
second .zip
Every file does have different GUID.Type as name. and those guid's are random and never shown while calling of extraction API or while getting its progress.
these are shown in below JSON file. Now suppose I extracted 10 different object files from one model. that means in bucket 30 new file URN will be generated.
And it is getting very hard to identify.
The Solution could have been if I extract obj geometry from model, I should get that particular obj, mtl, zips guid. IF I am getting only 3 ids then I can map those and download it from bucket.
What is happening now is I am getting all obj's guid (suppose 30 ) after calling get manifest GET :urn/manifest . And I am not understanding what obj file belong to which extraction.
{
"type": "manifest",
"hasThumbnail": "true",
"status": "success",
"progress": "complete",
"region": "US",
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA",
"version": "1.0",
"derivatives": [{
"name": "rme_advanced_sample_project.nwd",
"hasThumbnail": "true",
"status": "success",
"progress": "complete",
"outputType": "svf",
"children": [{
"guid": "763d96f1-0812-451d-b3a1-a51a27eba53b",
"name": "rme_advanced_sample_project.rvt",
"hasThumbnail": "true",
"role": "3d",
"viewableID": "rme_advanced_sample_project.rvt",
"progress": "complete",
"useAsDefault": true,
"status": "success",
"type": "geometry",
"children": [{
"guid": "1e172e73-2305-4eb8-9954-eeab8217a8d3",
"name": "Default",
"hasThumbnail": "true",
"role": "3d",
"camera": [-86.09573364257812, -79.89456176757812, 139.12220764160156, 70.56798553466797, 76.76915740966797, -17.541505813598633, 0.40824830532073975, 0.40824830532073975, 0.8164966106414795, 1.664323329925537, 0.785398006439209, 1, 0],
"useAsDefault": true,
"status": "success",
"type": "view",
"children": [{
"guid": "f2054c5f-10b8-457b-a927-23b69107dcd7",
"role": "thumbnail",
"mime": "image/png",
"resolution": [100, 100],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/0/0_100.png",
"type": "resource"
}, {
"guid": "20625548-d014-4d29-8e50-4d61a1759a60",
"role": "thumbnail",
"mime": "image/png",
"resolution": [200, 200],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/0/0_200.png",
"type": "resource"
}, {
"guid": "48675a6a-f2ec-4748-b22c-200b72cd06a8",
"role": "thumbnail",
"mime": "image/png",
"resolution": [400, 400],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/0/0_400.png",
"type": "resource"
}]
}, {
"guid": "77850512-9e8b-4d09-bb1a-45706ad5b7e6",
"name": "3D View",
"hasThumbnail": "false",
"role": "viewable",
"progress": "0% complete",
"status": "success",
"type": "folder",
"children": [{
"guid": "ebf9f756-6cfa-4858-9787-e3af9c8424c3",
"name": "3D Duct & Equipment",
"role": "3d",
"camera": [61.56271743774414, 38.53883743286133, 9.783880233764648, 59.46215057373047, 40.639404296875, 7.683313846588135, -0.40824830532073975, 0.40824830532073975, 0.8164966106414795, 1, 45, 227.96743774414062, 1],
"status": "success",
"type": "view"
}, {
"guid": "54d5a270-f330-4446-a482-1079312d8518",
"name": "Main Electrical Distribution",
"role": "3d",
"camera": [91.54497528076172, -63.42673110961914, -34.64605712890625, 46.680015563964844, 38.5232048034668, -66.7026596069336, -0.11140129715204239, 0.25314533710479736, 0.9609928131103516, 1, 45, 342.1146545410156, 1],
"status": "success",
"type": "view"
}, {
"guid": "2c6938f2-c052-4195-93d0-adc7af1cb965",
"name": "3D Plumbing",
"role": "3d",
"camera": [3.6063644886016846, -184.62478637695312, 180.24473571777344, 173.09983825683594, -2.4171712398529053, -0.014680136926472187, 0.39955270290374756, 0.42952418327331543, 0.8098559379577637, 1, 45, 461.8291931152344, 1],
"status": "success",
"type": "view"
}, {
"guid": "1b3f6ae4-beb6-478c-9cfe-b83bf371141d",
"name": "Room 53 3D Fire Protection",
"role": "3d",
"camera": [735.119384765625, -538.998779296875, 662.6300048828125, 98.2931900024414, 97.82740020751953, 25.80381202697754, -0.40824830532073975, 0.40824830532073975, 0.8164966106414795, 1, 45, 368.90972900390625, 1],
"status": "success",
"type": "view"
}, {
"guid": "45811cc4-79ed-421b-82b2-6c8548d1507e",
"name": "WSHP 2-3 System View",
"role": "3d",
"camera": [108.588134765625, 117.7073974609375, 54.383365631103516, 102.33130645751953, 123.9642333984375, 48.12653350830078, -0.40824830532073975, 0.40824830532073975, 0.8164966106414795, 1, 45, 333.1808776855469, 1],
"status": "success",
"type": "view"
}, {
"guid": "8c1402a9-6af6-47af-af0d-2c025ca26109",
"name": "Plumbing Isometric - Domestic Water",
"role": "3d",
"camera": [3.6063644886016846, -184.62478637695312, 180.24473571777344, 173.09983825683594, -2.4171712398529053, -0.014680136926472187, 0.39955270290374756, 0.42952418327331543, 0.8098559379577637, 1, 45, 461.8291931152344, 1],
"status": "success",
"type": "view"
}, {
"guid": "9dcc0f6a-fc2c-418f-8d08-c99b7afed337",
"name": "Plumbing Isometric - Sanitary Waste",
"role": "3d",
"camera": [3.6063644886016846, -184.62478637695312, 180.24473571777344, 173.09983825683594, -2.4171712398529053, -0.014680136926472187, 0.39955270290374756, 0.42952418327331543, 0.8098559379577637, 1, 45, 461.8291931152344, 1],
"status": "success",
"type": "view"
}, {
"guid": "e3a2443a-7f4c-49dc-a41d-da067922ab04",
"name": "Typical Make Up Air Isometric Detail",
"role": "3d",
"camera": [0.8279496431350708, 187.99771118164062, 118.81455993652344, 100.31283569335938, 88.51282501220703, 19.32967758178711, 0.40824830532073975, -0.40824830532073975, 0.8164966106414795, 1, 45, 248.74505615234375, 1],
"status": "success",
"type": "view"
}, {
"guid": "915564b9-ed83-426e-88df-002dac32363b",
"name": "Typical Room WSHP",
"role": "3d",
"camera": [171.6078338623047, -3.038959264755249, 18.71452522277832, 90.31718444824219, 43.15993118286133, -21.66922950744629, -0.34472253918647766, 0.19591180980205536, 0.9180331826210022, 1, 45, 257.0640869140625, 1],
"status": "success",
"type": "view"
}, {
"guid": "12a3ef04-1364-4c23-af91-2679132e3848",
"name": "Toilet Room",
"role": "3d",
"camera": [196.7369842529297, -4.305071830749512, 109.12708282470703, 98.9510269165039, 86.86759948730469, 14.070858001708984, -0.42381933331489563, 0.3951563239097595, 0.8150022625923157, 1, 45, 359.9964294433594, 1],
"status": "success",
"type": "view"
}, {
"guid": "9a34ab86-dfdf-4516-a087-b8f9775435fe",
"name": "1st Level Slice",
"role": "3d",
"camera": [-76.2143325805664, -163.60708618164062, 175.46923828125, 111.73212432861328, 69.6202392578125, 11.465206146240234, 0.3013473451137543, 0.373949259519577, 0.8771269917488098, 1, 45, 313.0205993652344, 1],
"status": "success",
"type": "view"
}, {
"guid": "09effac0-27ac-4f02-b4d7-31ef18a57ef9",
"name": "{3D}",
"role": "3d",
"camera": [0.8279496431350708, 187.99771118164062, 118.81455993652344, 100.31283569335938, 88.51282501220703, 19.32967758178711, 0.40824830532073975, -0.40824830532073975, 0.8164966106414795, 1, 45, 358.7159118652344, 1],
"status": "success",
"type": "view"
}]
}, {
"guid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"role": "graphics",
"mime": "application/autodesk-svf",
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/0/0.svf",
"type": "resource"
}]
}, {
"guid": "3b7195a7-17ec-4d80-b259-4e9df165f3fb",
"role": "Autodesk.CloudPlatform.PropertyDatabase",
"mime": "application/autodesk-db",
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/0/properties.db",
"status": "success",
"type": "resource"
}]
}, {
"hasThumbnail": "false",
"status": "success",
"progress": "complete",
"outputType": "obj",
"children": [{
"guid": "22519ef5-9da5-35ca-bc70-b25b45e8f8fa",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [547, 924, 942, 977],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/e72fa6b3-4fc2-3c86-8424-5d0755539c0d.obj",
"status": "success",
"type": "resource"
}, {
"guid": "75401422-bb33-350a-aa10-05625504d6ca",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [547, 924, 942, 977],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/e72fa6b3-4fc2-3c86-8424-5d0755539c0d.mtl",
"status": "success",
"type": "resource"
}, {
"guid": "a4b9fb43-8525-3243-bde1-72797399e342",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [547, 924, 942, 977],
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/e72fa6b3-4fc2-3c86-8424-5d0755539c0d.zip",
"status": "success",
"type": "resource"
}, {
"guid": "1177e7ea-7fad-34b7-a7bd-23d70e4a62a8",
"type": "resource",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [17, 19, 21, 22, 23, 24, 25, 26, 27, 83, 84, 85, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 263, 264, 265, 268, 269, 270, 302, 303, 304, 320, 321, 322, 350, 351, 352, 370, 371],
"status": "success",
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/a08e65e7-082a-3ce1-8116-ae534c9f081d.obj"
}, {
"guid": "61acd067-4637-3549-9bd3-cd362a4fb93b",
"type": "resource",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [17, 19, 21, 22, 23, 24, 25, 26, 27, 83, 84, 85, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 263, 264, 265, 268, 269, 270, 302, 303, 304, 320, 321, 322, 350, 351, 352, 370, 371],
"status": "success",
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/a08e65e7-082a-3ce1-8116-ae534c9f081d.mtl"
}, {
"guid": "6c73254c-7bbf-312a-a481-aaeebdf53de3",
"type": "resource",
"role": "obj",
"modelGuid": "f9f4721d-9304-43c4-8c07-34cbcc5e79c7",
"objectIds": [17, 19, 21, 22, 23, 24, 25, 26, 27, 83, 84, 85, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 263, 264, 265, 268, 269, 270, 302, 303, 304, 320, 321, 322, 350, 351, 352, 370, 371],
"status": "success",
"urn": "urn:adsk.viewing:fs.file:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXh0cmFjdC1hdXRvZGVzay1pby0yMDE3MnVvbm5paWJkd2t1Z25ydGVvZGFjN2FpeWhwdjdrd20vcm1lX2FkdmFuY2VkX3NhbXBsZV9wcm9qZWN0Lm53ZA/output/geometry/a08e65e7-082a-3ce1-8116-ae534c9f081d.zip"
}]
}]
}
Use GET :urn/metadata to obtain a list of model views:
{
"data": {
"type": "metadata",
"metadata": [
{
"name": "Scene",
"role": "3d",
"guid": "4f981e94-8241-4eaf-b08b-cd337c6b8b1f",
},
{
"name": "Sheet: A101",
"role": "2d",
"guid": "8e7c6bca-cfd1-290e-4b16-f3670169bb71"
}
]
}
}
Then use GET :urn/metadata/:guid to retrieve the object tree to identify your derivatives:
{
"data": {
"type": "objects",
"objects": [
{
"objectid": 1,
"name": "A5",
"objects": [
{
"objectid": 2,
"name": "Model",
"objects": [
{
"objectid": 3,
"name": "Bottom",
"objects": [
{
"objectid": 4,
"name": "Box"
}
]
},
root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
I have an object array like tree structure like above, I want to get all unique paths in like this
Food->Dry Food Items->Local Dry Food Items
Food->Dry Food Items->Thai Dry Food Items
Food->Dry Food Items->Others
Food->Fruits
------
------
This is my object
[
{
"id": 1,
"name": "Food",
"parent_id": 0,
"children": [
{
"id": 5,
"name": "Dry Food Items",
"parent_id": 1,
"children": [
{
"id": 11,
"name": "Local Dry Food Items",
"parent_id": 5
},
{
"id": 12,
"name": "Thai Dry Food Items",
"parent_id": 5
},
{
"id": 60,
"name": "Others",
"parent_id": 5
}
]
},
{
"id": 6,
"name": "Fruits",
"parent_id": 1
},
{
"id": 7,
"name": "LG Branded",
"parent_id": 1
},
{
"id": 8,
"name": "Meat",
"parent_id": 1
},
{
"id": 9,
"name": "Sea food",
"parent_id": 1
},
{
"id": 10,
"name": "Vegetables",
"parent_id": 1,
"children": [
{
"id": 14,
"name": "Local Vegetables",
"parent_id": 10
},
{
"id": 15,
"name": "Thai Vegetables",
"parent_id": 10
}
]
},
{
"id": 38,
"name": "Frozen",
"parent_id": 1
},
{
"id": 39,
"name": "IP Kitchen",
"parent_id": 1,
"children": [
{
"id": 40,
"name": "IP Meat",
"parent_id": 39
},
{
"id": 41,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 42,
"name": "IP Ingredients",
"parent_id": 39
},
{
"id": 43,
"name": "IP Sauce",
"parent_id": 39
},
{
"id": 44,
"name": "IP Seafood",
"parent_id": 39
},
{
"id": 45,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 46,
"name": "IP Desert",
"parent_id": 39
}
]
}
]
},
{
"id": 2,
"name": "Beverage",
"parent_id": 0,
"children": [
{
"id": 16,
"name": "Bar",
"parent_id": 2
},
{
"id": 17,
"name": "Coffee & Tea",
"parent_id": 2
},
{
"id": 18,
"name": "In Can",
"parent_id": 2
},
{
"id": 19,
"name": "Water",
"parent_id": 2
},
{
"id": 47,
"name": "IP Bar",
"parent_id": 2
}
]
},
{
"id": 3,
"name": "Disposable",
"parent_id": 0,
"children": [
{
"id": 21,
"name": "Disposable",
"parent_id": 3
}
]
},
{
"id": 4,
"name": "SOE",
"parent_id": 0,
"children": [
{
"id": 20,
"name": "Cleaning Materials",
"parent_id": 4
},
{
"id": 22,
"name": "Chinaware",
"parent_id": 4
}
]
}
];
I get to all the nodes in the tree
function traverse(categories) {
categories.forEach(function (category) {
if (category.children && category.children.length) {
traverse(category.children);
}
else {
}
}, this);
}
You can use recursion and create a function using forEach loop.
var arr = [{"id":1,"name":"Food","parent_id":0,"children":[{"id":5,"name":"Dry Food Items","parent_id":1,"children":[{"id":11,"name":"Local Dry Food Items","parent_id":5},{"id":12,"name":"Thai Dry Food Items","parent_id":5},{"id":60,"name":"Others","parent_id":5}]},{"id":6,"name":"Fruits","parent_id":1},{"id":7,"name":"LG Branded","parent_id":1},{"id":8,"name":"Meat","parent_id":1},{"id":9,"name":"Sea food","parent_id":1},{"id":10,"name":"Vegetables","parent_id":1,"children":[{"id":14,"name":"Local Vegetables","parent_id":10},{"id":15,"name":"Thai Vegetables","parent_id":10}]},{"id":38,"name":"Frozen","parent_id":1},{"id":39,"name":"IP Kitchen","parent_id":1,"children":[{"id":40,"name":"IP Meat","parent_id":39},{"id":41,"name":"IP Starter","parent_id":39},{"id":42,"name":"IP Ingredients","parent_id":39},{"id":43,"name":"IP Sauce","parent_id":39},{"id":44,"name":"IP Seafood","parent_id":39},{"id":45,"name":"IP Starter","parent_id":39},{"id":46,"name":"IP Desert","parent_id":39}]}]},{"id":2,"name":"Beverage","parent_id":0,"children":[{"id":16,"name":"Bar","parent_id":2},{"id":17,"name":"Coffee & Tea","parent_id":2},{"id":18,"name":"In Can","parent_id":2},{"id":19,"name":"Water","parent_id":2},{"id":47,"name":"IP Bar","parent_id":2}]},{"id":3,"name":"Disposable","parent_id":0,"children":[{"id":21,"name":"Disposable","parent_id":3}]},{"id":4,"name":"SOE","parent_id":0,"children":[{"id":20,"name":"Cleaning Materials","parent_id":4},{"id":22,"name":"Chinaware","parent_id":4}]}]
function getNames(data) {
var result = [];
function loop(data, c) {
data.forEach(function (e) {
var name = !c.length ? e.name : c + '->' + e.name;
if (e.children) { loop(e.children, name); }
else {
result.push({ name: name });
}
});
}
loop(data, '');
return result;
}
console.log(getNames(arr))
I'm using AngularJS and the jquery chosen plugin to populate a multiple select form. My Angular code has a service that is calling a node.js web service. The option list for the chosen select box is being populated from a json file while the value is being stored within model from the nodejs service. I've been using this link to guide me but now seem to be stuck.
I've included the code here.
My chosen options are being populated as such:
[
{"id": 1, "name": "00:00", "value": 0},
{"id": 2, "name": "00:15", "value": 900000},
{"id": 3, "name": "00:30", "value": 1800000}
...
]
But my model is being stored as:
meal.dinnerTimes = ["06:15","18:30"]
So when the model is loaded by the service and controller. The select box is always blank where I would expect to see
"06:15" and "18:30" already populated in this case
Any help would be appreciated
As you only have value with you while assigning model, use select as label for value in array syntax to have only value property assigned to the model.
Also note that you are not invoking callback function provided in MealSvc factory hence, value of model will never get set!
Plunker Demo
var app = angular.module("MealApp", ['MealService']);
var data = [{
"id": 1,
"name": "00:00",
"value": 0
}, {
"id": 2,
"name": "00:15",
"value": 900000
}, {
"id": 3,
"name": "00:30",
"value": 1800000
}, {
"id": 4,
"name": "00:45",
"value": 2700000
}, {
"id": 5,
"name": "01:00",
"value": 3600000
}, {
"id": 6,
"name": "01:15",
"value": 4500000
}, {
"id": 7,
"name": "01:30",
"value": 5400000
}, {
"id": 8,
"name": "01:45",
"value": 6300000
}, {
"id": 9,
"name": "02:00",
"value": 7200000
}, {
"id": 10,
"name": "02:15",
"value": 8100000
}, {
"id": 11,
"name": "02:30",
"value": 9000000
}, {
"id": 12,
"name": "02:45",
"value": 9900000
}, {
"id": 13,
"name": "03:00",
"value": 10800000
}, {
"id": 14,
"name": "03:15",
"value": 11700000
}, {
"id": 15,
"name": "03:30",
"value": 12600000
}, {
"id": 16,
"name": "03:45",
"value": 13500000
}, {
"id": 17,
"name": "04:00",
"value": 14400000
}, {
"id": 18,
"name": "04:15",
"value": 15300000
}, {
"id": 19,
"name": "04:30",
"value": 16200000
}, {
"id": 20,
"name": "04:45",
"value": 17100000
}, {
"id": 21,
"name": "05:00",
"value": 18000000
}, {
"id": 22,
"name": "05:15",
"value": 18900000
}, {
"id": 23,
"name": "05:30",
"value": 19800000
}, {
"id": 24,
"name": "05:45",
"value": 20700000
}, {
"id": 25,
"name": "06:00",
"value": 21600000
}, {
"id": 26,
"name": "06:15",
"value": 22500000
}, {
"id": 27,
"name": "06:30",
"value": 23400000
}, {
"id": 28,
"name": "06:45",
"value": 24300000
}, {
"id": 29,
"name": "07:00",
"value": 25200000
}, {
"id": 30,
"name": "07:15",
"value": 26100000
}, {
"id": 31,
"name": "07:30",
"value": 27000000
}, {
"id": 32,
"name": "07:45",
"value": 27900000
}, {
"id": 33,
"name": "08:00",
"value": 28800000
}, {
"id": 34,
"name": "08:15",
"value": 29700000
}, {
"id": 35,
"name": "08:30",
"value": 30600000
}, {
"id": 36,
"name": "08:45",
"value": 31500000
}, {
"id": 37,
"name": "09:00",
"value": 32400000
}, {
"id": 38,
"name": "09:15",
"value": 33300000
}, {
"id": 39,
"name": "09:30",
"value": 34200000
}, {
"id": 40,
"name": "09:45",
"value": 35100000
}, {
"id": 41,
"name": "10:00",
"value": 36000000
}, {
"id": 42,
"name": "10:15",
"value": 36900000
}, {
"id": 43,
"name": "10:30",
"value": 37800000
}, {
"id": 44,
"name": "10:45",
"value": 38700000
}, {
"id": 45,
"name": "11:00",
"value": 39600000
}, {
"id": 46,
"name": "11:15",
"value": 40500000
}, {
"id": 47,
"name": "11:30",
"value": 41400000
}, {
"id": 48,
"name": "11:45",
"value": 42300000
}, {
"id": 49,
"name": "12:00",
"value": 43200000
}, {
"id": 50,
"name": "12:15",
"value": 44100000
}, {
"id": 51,
"name": "12:30",
"value": 45000000
}, {
"id": 52,
"name": "12:45",
"value": 45900000
}, {
"id": 53,
"name": "13:00",
"value": 46800000
}, {
"id": 54,
"name": "13:15",
"value": 47700000
}, {
"id": 55,
"name": "13:30",
"value": 48600000
}, {
"id": 56,
"name": "13:45",
"value": 49500000
}, {
"id": 57,
"name": "14:00",
"value": 50400000
}, {
"id": 58,
"name": "14:15",
"value": 51300000
}, {
"id": 59,
"name": "14:30",
"value": 52200000
}, {
"id": 60,
"name": "14:45",
"value": 53100000
}, {
"id": 61,
"name": "15:00",
"value": 54000000
}, {
"id": 62,
"name": "15:15",
"value": 54900000
}, {
"id": 63,
"name": "15:30",
"value": 55800000
}, {
"id": 64,
"name": "15:45",
"value": 56700000
}, {
"id": 65,
"name": "16:00",
"value": 57600000
}, {
"id": 66,
"name": "16:15",
"value": 58500000
}, {
"id": 67,
"name": "16:30",
"value": 59400000
}, {
"id": 68,
"name": "16:45",
"value": 60300000
}, {
"id": 69,
"name": "17:00",
"value": 61200000
}, {
"id": 70,
"name": "17:15",
"value": 62100000
}, {
"id": 71,
"name": "17:30",
"value": 63000000
}, {
"id": 72,
"name": "17:45",
"value": 63900000
}, {
"id": 73,
"name": "18:00",
"value": 64800000
}, {
"id": 74,
"name": "18:15",
"value": 65700000
}, {
"id": 75,
"name": "18:30",
"value": 66600000
}, {
"id": 76,
"name": "18:45",
"value": 67500000
}, {
"id": 77,
"name": "19:00",
"value": 68400000
}, {
"id": 78,
"name": "19:15",
"value": 69300000
}, {
"id": 79,
"name": "19:30",
"value": 70200000
}, {
"id": 80,
"name": "19:45",
"value": 71100000
}, {
"id": 81,
"name": "20:00",
"value": 72000000
}, {
"id": 82,
"name": "20:15",
"value": 72900000
}, {
"id": 83,
"name": "20:30",
"value": 73800000
}, {
"id": 84,
"name": "20:45",
"value": 74700000
}, {
"id": 85,
"name": "21:00",
"value": 75600000
}, {
"id": 86,
"name": "21:15",
"value": 76500000
}, {
"id": 87,
"name": "21:30",
"value": 77400000
}, {
"id": 88,
"name": "21:45",
"value": 78300000
}, {
"id": 89,
"name": "22:00",
"value": 79200000
}, {
"id": 90,
"name": "22:15",
"value": 80100000
}, {
"id": 91,
"name": "22:30",
"value": 81000000
}, {
"id": 92,
"name": "22:45",
"value": 81900000
}, {
"id": 93,
"name": "23:00",
"value": 82800000
}, {
"id": 94,
"name": "23:15",
"value": 83700000
}, {
"id": 95,
"name": "23:30",
"value": 84600000
}, {
"id": 96,
"name": "23:45",
"value": 85500000
}];
app.directive('chosen', function() {
var linker = function(scope, element, attr) {
scope.$watch('availableTimes', function() {
element.triggerHandler('chosen:updated');
});
scope.$watch('MealSvc.get()', function() {
element.triggerHandler('chosen:updated');
});
element.chosen({
width: "95%"
});
};
return {
restrict: 'A',
link: linker
}
});
app.controller("MealCtrl", function MealCtrl($scope, $window, $http, MealSvc) {
$scope.times = [];
$scope.availableTimes = [];
$scope.fetchTimes = function() {
$scope.availableTimes = data;
}
$scope.fetchTimes();
MealSvc.get(function(res) {
$scope.times = res.dinnerTimes;
});
});
angular.module('MealService', []).factory('MealSvc', function($http) {
return {
get: function(response) {
response({
"name": "Second Tea",
"dinnerTimes": [46800000, 57600000]
})
}
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" data-semver="2.2.0" data-require="jquery#*"></script>
<link data-require="chosen#*" data-semver="1.0.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.min.css" />
<script data-require="chosen#*" data-semver="1.0.0" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.jquery.min.js"></script>
<script data-require="chosen#*" data-semver="1.0.0" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.proto.min.js"></script>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js" data-require="angular.js#1.2.x"></script>
<script src="app.js"></script>
</head>
<body>
<div class="container" ng-app="MealApp" ng-controller="MealCtrl">
<div class="row">
<select data-placeholder="Select Dinner Time" multiple class="chzn-select" chosen ng-model="times" ng-options="times.value as times.name for times in availableTimes"></select>
</div>
</div>
</body>
</html>