JS: merging 2 arrays within an object - javascript

Given a JSON object, how would you merge the batter array and topping array within the object. I know I can do let x = foo.batters.batter.concat(foo.topping), which will give me an array of the merged array but what if I wanted it within object?
foo = {
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}

Your answer is partially true. But you didn't add the value to the foo object.
You can do it like this:
foo.mergedArray = foo.batters.batter.concat(foo.topping);
After that, when you call foo.mergedArray you can access merged array of batter and topping.
If one of the batter and topping has changed foo.mergedArray will not be updated. Because foo.mergedArray has its own referance.

Try this:
foo.batters.batter = [...foo.batters.batter, ...foo.topping];

Related

Mapping/digging into an array that is nested in an object - JavaScript

This is my first question on stack overflow. I am attempting to map an array that is housed within an object. My question is more regarding the targeting than the actual mapping process itself (I think). The goal of my code is to map an array to this target:
var target = {
"id": 1, //as an int
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": "all of the batter types as a string",
"ingredients": [],//a copy of all the toppings
"countOfFillings": 0
}
the object in question is:
var bakery = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0004",
"type": "bar",
"name": "Bar",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7001", "name": "None", "addcost": 0 },
{ "id": "7002", "name": "Custard", "addcost": 0.25 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0.25 }
]
}
},
{
"id": "0005",
"type": "twist",
"name": "Twist",
"ppu": 0.65,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
]
},
{
"id": "0006",
"type": "filled",
"name": "Filled",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7002", "name": "Custard", "addcost": 0 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0 },
{ "id": "7004", "name": "Strawberry Jelly", "addcost": 0 },
{ "id": "7005", "name": "Rasberry Jelly", "addcost": 0 }
]
}
}
]
}
}
And this is my attempt at mapping. I'm able to get the array generated but only from the first item in the array as indicated with the [0]. Any feedback or advice would be greatly appreciated!
var exampleTest = bakery.items.item.map(mapCake);
function mapCake (aCake)
{
let newType = `${bakery.items.item[1].type}`
let result = {id: `${bakery.items.item[0].id}`,
type: newType,
name: `${bakery.items.item[0].name}`,
ppu: `${bakery.items.item[0].ppu}`,
batters: [`${bakery.items.item[0].batters.batter}`],
ingredients: `${bakery.items.item[0].topping}`,
countOfFillings: `${bakery.items.item[0].fillings}`.length};
return result
}
Try this:
function mapCake(aCake) {
let newType = `${aCake.type}`;
let result = {
id: `${aCake.id}`,
type: newType,
name: `${aCake.name}`,
ppu: `${aCake.ppu}`,
batters: [`${aCake.batters.batter}`],
ingredients: `${aCake.topping}`,
countOfFillings: `${aCake.fillings}`.length
};
return result;
}
Looks like in your function you're directly indexing a specific bakery item each time.
Your aCake parameter wasn't yet being used, at each iteration, aCake refers to the current item that is being iterated over - so that's what you want to be populating your result values with.

How to loop through an array of objects to find a user's id?

how can I loop through this JSON in JavaScript and check whether a user's id exists? This data is from an image annotation collection and I am going to need to prevent a user from editing someone else's annotations.
In this example, lets just say that user id I am looking for is K2Lb1R7owqR9BYmpJAJzrg6w1s92 from the creator: key.
I tried using for example arr.includes() but didn't work for me.
What is my expected output? I think for now I need to just worry whether a user's id exists or not and if so, return false or true.
The creator object will always be present inside the body.
Thank you for your help.
[{
"body": [{
"created": "2020-07-03T11:49:32.058Z",
"purpose": "commenting",
"value": "don't move me",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
},
"type": "TextualBody"
}, {
"purpose": "tagging",
"value": "move",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"type": "TextualBody",
"created": "2020-07-03T11:49:30.850Z"
}],
"id": "IRCJPsz4NKkiuhVLE1hR",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"#context": "http://www.w3.org/ns/anno.jsonld",
"type": "Annotation",
"target": {
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0",
"selector": {
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector",
"value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"
}
}
}, {
"body": [{
"purpose": "commenting",
"type": "TextualBody",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"value": "anyone ever climb this mountain top?",
"created": "2020-07-03T10:57:40.590Z"
}, {
"creator": {
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92",
"name": "doss"
},
"purpose": "tagging",
"value": "ridge",
"type": "TextualBody",
"created": "2020-07-03T10:57:39.351Z"
}, {
"value": "i did in 2005",
"created": "2020-07-03T10:59:45.318Z",
"type": "TextualBody",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"purpose": "commenting"
}, {
"type": "TextualBody",
"created": "2020-07-03T10:59:43.966Z",
"purpose": "tagging",
"value": "testme",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
}
}, {
"type": "TextualBody",
"purpose": "replying",
"value": "test",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"created": "2020-07-03T11:39:18.860Z"
}],
"type": "Annotation",
"#context": "http://www.w3.org/ns/anno.jsonld",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"target": {
"selector": {
"value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector"
},
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
},
"id": "IlI7SRjFm8qohmHDLBw2"
}]
You can make use of flatMap to get flat data in array then using some to find out whether the value is present or not:
var array=[{ "body": [{ "created": "2020-07-03T11:49:32.058Z", "purpose": "commenting", "value": "don't move me", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" }, "type": "TextualBody" }, { "purpose": "tagging", "value": "move", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "type": "TextualBody", "created": "2020-07-03T11:49:30.850Z" }], "id": "IRCJPsz4NKkiuhVLE1hR", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "#context": "http://www.w3.org/ns/anno.jsonld", "type": "Annotation", "target": { "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0", "selector": { "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector", "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344" } }}, { "body": [{ "purpose": "commenting", "type": "TextualBody", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "value": "anyone ever climb this mountain top?", "created": "2020-07-03T10:57:40.590Z" }, { "creator": { "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92", "name": "doss" }, "purpose": "tagging", "value": "ridge", "type": "TextualBody", "created": "2020-07-03T10:57:39.351Z" }, { "value": "i did in 2005", "created": "2020-07-03T10:59:45.318Z", "type": "TextualBody", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "purpose": "commenting" }, { "type": "TextualBody", "created": "2020-07-03T10:59:43.966Z", "purpose": "tagging", "value": "testme", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" } }, { "type": "TextualBody", "purpose": "replying", "value": "test", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "created": "2020-07-03T11:39:18.860Z" }], "type": "Annotation", "#context": "http://www.w3.org/ns/anno.jsonld", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "target": { "selector": { "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156", "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector" }, "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0" }, "id": "IlI7SRjFm8qohmHDLBw2"}];
var result = array.flatMap(elem=>elem.body).some(user=>user.creator.id==='K2Lb1R7owqR9BYmpJAJzrg6w1s92');
console.log(result);
some will return either true or false, which is what you need here.
I hope this helps.
You can make use of the some function:
const data = [{"body":[{"created":"2020-07-03T11:49:32.058Z","purpose":"commenting","value":"don't move me","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"},"type":"TextualBody"},{"purpose":"tagging","value":"move","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"type":"TextualBody","created":"2020-07-03T11:49:30.850Z"}],"id":"IRCJPsz4NKkiuhVLE1hR","photoDocId":"92wNwz2aaqy7CWf3mGo1","#context":"http://www.w3.org/ns/anno.jsonld","type":"Annotation","target":{"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0","selector":{"conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector","value":"xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"}}},{"body":[{"purpose":"commenting","type":"TextualBody","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"value":"anyone ever climb this mountain top?","created":"2020-07-03T10:57:40.590Z"},{"creator":{"id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92","name":"doss"},"purpose":"tagging","value":"ridge","type":"TextualBody","created":"2020-07-03T10:57:39.351Z"},{"value":"i did in 2005","created":"2020-07-03T10:59:45.318Z","type":"TextualBody","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"purpose":"commenting"},{"type":"TextualBody","created":"2020-07-03T10:59:43.966Z","purpose":"tagging","value":"testme","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"}},{"type":"TextualBody","purpose":"replying","value":"test","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"created":"2020-07-03T11:39:18.860Z"}],"type":"Annotation","#context":"http://www.w3.org/ns/anno.jsonld","photoDocId":"92wNwz2aaqy7CWf3mGo1","target":{"selector":{"value":"xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156","conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector"},"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"},"id":"IlI7SRjFm8qohmHDLBw2"}]
console.log(data.some(({body}) => body.some(({creator}) => creator.id === 'K2Lb1R7owqR9BYmpJAJzrg6w1s92')));

Sorting Node's on the base of parent-Id [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
Want to sort my data all node come first before calling its parent node
[
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
}
]
want to update data like this using javascript
[
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
}
]
try this one
const sortedData = [
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
}
].sort(function(a,b){
if(a.parentId!= undefined && b.parentId != undefined){
return a.parentId > b.parentId
}
return false;
});
console.log(sortedData);

Best way to transform data

I have a vendor API call in my nodejs app that returns various objects, with the below being an example of one object in the the JSON:
[{
"id": 365467865567,
"type": "PERSON",
"created_time": 1492809120,
"updated_time": 1492809542,
"last_contacted": 1492809128,
"properties": [
{
"type": "SYSTEM",
"name": "phone",
"value": "123456789"
},
{
"type": "SYSTEM",
"name": "first_name",
"value": "Test2"
},
{
"type": "SYSTEM",
"name": "last_name",
"value": "You"
},
{
"type": "CUSTOM",
"name": "utm_medium",
"value": "email"
}
]
}]
Its not very easy to use the data in this format, and need to transpose it to a key,value format. Something similar to:
[{
"id": 365467865567,
"type": "PERSON",
"created_time": 1492809120,
"updated_time": 1492809542,
"last_contacted": 1492809128,
"properties":
{
"phone": "123456789",
"first_name": "Test2",
"last_name": "You",
"utm_medium": "email"
}
}]
What would be the most efficient way to transform this?
Try looping over the properties and reassigning the variable.
var array = [{
"id": 365467865567,
"type": "PERSON",
"created_time": 1492809120,
"updated_time": 1492809542,
"last_contacted": 1492809128,
"properties": [{
"type": "SYSTEM",
"name": "phone",
"value": "123456789"
}, {
"type": "SYSTEM",
"name": "first_name",
"value": "Test2"
}, {
"type": "SYSTEM",
"name": "last_name",
"value": "You"
}, {
"type": "CUSTOM",
"name": "utm_medium",
"value": "email"
}]
}];
array.forEach(function(obj) {
var properties = {};
obj.properties.forEach(function(prop) {
properties[prop.name] = prop.value;
});
obj.properties = properties;
});
https://jsfiddle.net/hycg5r80/3/
here's a way, idk if most efficient but it's short n sweet.
thing[0].properties = thing[0].properties.reduce( (p, n) => (
n[p.name] = p.value;
return n;
), {});

How to select specific object in JSON

I have JSON Data like below
{
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}]
}
}
I want to select item object having id 0001.So how can I select or filter only this single item object using javascript or Jquery??
please help me and thanks in advance
Like this:
var item = data.items.item.filter(function(x){
return x.id === '0001';
})[0];

Categories

Resources