How to iterate object when it contains array of objects? - arrays

Input :
{
"id": "123",
"address": [{
"street": "5",
"city": "ameerpet",
"pin": "500073"
}, {
"street": "6",
"city": "sec",
"pin": "500020"
}]
}
Note: ["LAA001","LAA002","LAA003"] -> use this as a variable
Required output:
[{
"id": "123",
"lob": "LAA001",
"attributeText": "5"
},
{
"id": "123",
"lob": "LAA001",
"attributeText": "6"
},
{
"id": "123",
"lob": "LAA002",
"attributeText": "ameerpet"
},
{
"id": "123",
"lob": "LAA002",
"attributeText": "sec"
},
{
"id": "123",
"lob": "LAA003",
"attributeText": "500073"
},
{
"id": "123",
"lob": "LAA003",
"attributeText": "500020"
}
]

If you are using JavaScript, that should work:
var input = { "id": "123", "address": [ { "street": "5", "city": "ameerpet", "pin": "500073" }, { "street": "6", "city": "sec", "pin": "500020" }] }
var iob = ["LAA001","LAA002","LAA003"]
var output = []
input['address'].forEach((item, index) => {
var keyIndex = 0
for(const key in item){
let obj = {
"id": input['id'],
"lob": iob[keyIndex],
"attributeText": item[key]
}
output.push(obj)
keyIndex += 1
}

%dw 2.0
output application/json
var inp = ["LAA001","LAA002","LAA003"]
var inp1 = payload.address.street ++ payload.address.city ++ payload.address.pin
---
inp1 map {
id: payload.id,
lob: inp[(($$)/2)],
attributeText: $
}

Related

How to access $value in a JSON?

{
"$id": "1",
"listTasks": {
"$id": "2",
"$values": [
{
"$id": "3",
"id": 1,
"name": "Task1G1",
"priorityID": 1,
"dueDate": "2022-07-14T04:12:14.114",
"createdAt": "2022-07-14T11:13:06.1808811",
"enumerable": {
"$id": "4",
"$values": [
2
]
}
},
{
"$id": "5",
"id": 2,
"name": "string",
"priorityID": 1,
"dueDate": "2022-07-29T08:55:06.156",
"createdAt": "2022-07-14T15:55:57.0330615",
"enumerable": {
"$id": "6",
"$values": [
3
]
}
}
]
}
}
I want to access the secondary $value in this json (enumerable object), I use this code below:
useEffect(() => {
axios
.get(API_URL + Task/GetTaskInGroup?GroupID=${id}, {})
.then((response) => {
**setTaskList(response.data.listTasks.$values.enumerable.$value);**
})
.catch((error) => {
console.log(error.response.data);
});
}, [id]);
But it didn't work, I tried setTaskList(response.data.listTasks.$values) then it work (I can get all data but I can't access to enumerable object)
Try with computed property names (myObject[..]) like below:
response.data.listTasks["$values"][0].enumerable["$values"]
const data = { "$id": "1", "listTasks": { "$id": "2", "$values": [ { "$id": "3", "id": 1, "name": "Task1G1", "priorityID": 1, "dueDate": "2022-07-14T04:12:14.114", "createdAt": "2022-07-14T11:13:06.1808811", "enumerable": { "$id": "4", "$values": [ 2 ] } }, { "$id": "5", "id": 2, "name": "string", "priorityID": 1, "dueDate": "2022-07-29T08:55:06.156", "createdAt": "2022-07-14T15:55:57.0330615", "enumerable": { "$id": "6", "$values": [ 3 ] } } ] } }
console.log(data.listTasks["$values"][0].enumerable["$values"]);

AngularJS multiple Scope needed for Autocomplete

I have an array structure like this.
[
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
]
I need $scope like below for my autocomplete search.
$scope.name=["john","Gerold","Stuart"];
$scope.city=["NY","LA","Boston"];
can anyone help to get this using angularjs controller.
Thanks in Advance.!
Use MAP
$scope.users = [
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
];
$scope.cities = $scope.users.map(function(obj){
return obj.city;
});
console.log($scope.cities);
You can also create a helper function that would do that for you and you don't have to define a map per function that you want, and you do it in just one run (hence just a bit faster)
Sample here ;)
var myArray = [
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
]
function toScope(scopedPropertieNames, array) {
scopedPropertieNames.forEach(function(propertyName) {
if (! $scope[propertyName]) {
$scope[propertyName] = []
}
});
array.forEach(function (objecInArray) {
scopedPropertieNames.forEach(function(propertyName) {
$scope[propertyName].push(objecInArray[propertyName])
})
});
}
toScope(['name', 'city'], myArray);
console.log($scope) //{name: Array[3], city: Array[3]}
You can use MAP..
$scope.YourBigArray = [{
"id": "1",
"name": "John",
"city": "NY"
}, {
"id": "2",
"name": "Gerold",
"city": "LA"
}, {
"id": "3",
"name": "Stuart",
"city": "Boston"
}];
$scope.names = $scope.YourBigArray.map(function(object) {
return object.name;
});
$scope.cities = $scope.YourBigArray.map(function(object) {
return object.city;
});
You can do a filter to use unique things in array of names and cities..
function filterForDuplicate(things) {
return things.filter(function(item, pos) {
return things.indexOf(item) === pos;
});
}

How to access a single object from a JSON array

Okay so I have a JSON:
{
"Concepts": [
{
"Concept": "1",
"Description": "siopao"
},
{
"Concept": "4",
"Description": "gulaman"
},
{
"Concept": "9",
"Description": "sisig"
},
{
"Concept": "12",
"Description": "noodle"
},
{
"Concept": "15",
"Description": "sisigan"
},
{
"Concept": "16",
"Description": "buko shake"
},
{
"Concept": "17",
"Description": "mango shake"
},
{
"Concept": "19",
"Description": "burger"
},
{
"Concept": "20",
"Description": "sample"
},
{
"Concept": "21",
"Description": "shit"
}
]
}
How do I get only "Description":"siopao"? I'm using AngularJS.
If you want to map the jsonstring to something like:
["siopao", "gulaman", "sisig", "noodle", "sisigan", "buko shake", "mango shake", "burger", "sample", "shit"]
use
var object = JSON.parse(jsonString);
var descriptions = object.Concepts.map(function(c) {
return o.Description;
});
If you only want the one with the description "siopao", add a filter before the map:
var object = JSON.parse(jsonString);
var description = object.Concepts.filter(function(a) {
return a.Description === "siopao";
}).map(function(c) {
return o.Description;
})[0];

Angular Differences Between Two Models

I have an angular application which uses a significantly large shared model. Currently, when a user presses save the entire model is posted to a RESTful service. Ideally I would only like to post the fields that have changed. Since this is a shared model I do not have access to form validation states such as dirty/pristine etc. The idea I can think of is to have two models, the original and the modified and compare these.
Original Model
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "212 555-1234"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Changed Model
{
"firstName": "Jane",
"lastName": "Smith",
"isAlive": true,
"age": 50,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"id": "123",
"number": "1234567890"
},
{
"id": "456",
"number": "646 555-4567"
},
{
"id": "789",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
Data Posted - This is what I need!
{
"firstName": "Jane",
"age": 50,
"phoneNumbers": [
{
"id":"123",
"number": "1234567890"
}
]
}
How can I achieve this? I need the changed fields including any fields called id!
You'll need something like this. Working Fiddle: https://jsfiddle.net/jyyyLaot/
function filter(obj1, obj2) {
var result = {};
for(key in obj1) {
if(obj2[key] != obj1[key]) result[key] = obj2[key];
if(typeof obj2[key] == 'array' && typeof obj1[key] == 'array')
result[key] = arguments.callee(obj1[key], obj2[key]);
if(typeof obj2[key] == 'object' && typeof obj1[key] == 'object')
result[key] = arguments.callee(obj1[key], obj2[key]);
}
return result;
}

How can I loop through nested json and get array unique value from field

I got this format in json.
I need unique values of "volume". So in result I'd get only '30' and '40'.
I used loop inside of another loop, but it returns unique values from each product, which is not what I want.
I'm using Angular 1 in Ionic.
{
"data": [
{
"id": 1,
"title": "Category title",
"products": [
{
"id": 1,
"title": "product title",
"volume": "40"
},
{
"id": 2,
"title": "product title",
"volume": "30"
}
]
},
{
"id": 2,
"title": "Another Category title",
"products": [
{
"id": 3,
"title": "product title",
"volume": "40"
},
{
"id": 3,
"title": "product title",
"volume": "30"
}
]
}
]
}
Thanks
Try this it will work :
JSON :
var obj = {
"data": [{
"id": 1,
"title": "Category title",
"products": [{
"id": 1,
"title": "product title",
"volume": "40"
}, {
"id": 2,
"title": "product title",
"volume": "30"
}]
},
{
"id": 2,
"title": "Another Category title",
"products": [{
"id": 3,
"title": "product title",
"volume": "40"
}, {
"id": 3,
"title": "product title",
"volume": "30"
}]
}
]
};
Logic implementation :
var arr = [];
for (var item in obj.data) {
for (var innData in obj.data[item].products) {
var volume = obj.data[item].products[innData].volume;
if(arr.indexOf(volume) == '-1') {
arr.push(volume);
}
}
}
console.log(arr);
Output :
Working fiddle : https://jsfiddle.net/bo2drv5x/
To get the unique values
var uniqueVolumes = $.unique($.map(json.data, function(datum) {
return datum.products.map(function(product) {
return product.volume;
});
});
);
Using core java script you can find like this:
var jsonData = {"data": [
{
"id": 1,
"title": "Category title",
"products": [
{
"id": 1,
"title": "product title",
"volume": "40"
},
{
"id": 2,
"title": "product title",
"volume": "30"
}
]
},
{
"id": 2,
"title": "Another Category title",
"products": [
{
"id": 3,
"title": "product title",
"volume": "40"
},
{
"id": 3,
"title": "product title",
"volume": "30"
}
]
}
]
};
var uniqueArr = [];
var jsonData = jsonData["data"];
var fullObjectLength = Object.keys(jsonData).length
for(var i=0; i<fullObjectLength;i++)
{
//console.log(jsonData[i]["products"]);
var jsonProductsLength = jsonData[i]["products"].length;
for(var j=0; j<jsonProductsLength; j++)
{
var volumeKeyValue = jsonData[i]["products"][j]["volume"];
var itemTobePushedInArray = uniqueArr.indexOf(volumeKeyValue) == -1;
if(itemTobePushedInArray)
{
uniqueArr.push(volumeKeyValue);
console.log(uniqueArr);
}
}
}

Resources