Pull value from JSON key value pair using AngularJS - angularjs

Is there a nice way in Angular's ngRepeat to pull a value out of a JSON Array depending on its key. I working with the following JSON structure and would like to output the associated value of "Name 2" for each object.
{
"Items": [
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
}

try like this
angular.forEach(data.Items,function(value,key){
angular.forEach(value,function(v){
if(v.name == "Name 2")
console.log(v);
}
})

Here is an example. It will print the Value for "Name 2":
<body ng-app="myApp">
<div ng-controller="myCtrl">
<div ng-repeat="item in records.Items">
<div ng-repeat="singleAttribute in item.Attributes">
<div ng-if="singleAttribute.Name === 'Name 2'">
{{singleAttribute.Value}}
</div>
</div>
</div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = {
"Items": [
{
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
};
});
</script>
</body>

Related

How to change the value of a state object?

I created an add button for adding new objects to the state. The logic is firstly, copy the last object of the state and after add at the end of it.
I can write the code so far but the problem is, I have to change the value and the key of the objects.
Note: I have to change the keys because I'm creating input fields with the keys and they should be unique.
This is my code :
addField() {
const index = (this.state?.fields.length)
console.log("state: ", this.state)
const newFields = [this.state.fields[index]].fill(this.state.fields[index - 1])
this.setState(
{
fields: [...this.state.fields, ...newFields],
},
() => {
console.log("updated state", this.state);
}
);
}
outputs:
state:
{
"fields": [
[
{
"key": "input_field_name",
"value": "This is a value"
},
{
"key": "field_name",
"value": "field name"
},
{
"key": "datatype",
"value": "text"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
]
]
}
updated state:
{
"fields": [
[
{
"key": "input_field_name",
"value": "This is a value"
},
{
"key": "field_name",
"value": "field name"
},
{
"key": "datatype",
"value": "text"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
]
]
}
my state:
interface IDetailsPageState {
fields: IFieldDefinition[][];
activeFields: {
key: number;
fields: IFieldDefinition[];
};
}
How can I change the key and value of these objects?
<div key={`${this.state.activeFields.key}-${field.key}`}>
<TextInput config={inputConfig} bindings={inputBindings}></TextInput>
</div>

In Azure Search how can I filter records that have tags with IDs matching any in a list

Given the following search index document schema:
{
"value": [
{
"Id": "abc",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "456",
"Name": "Tag456"
}
]
},
{
"Id": "xyz",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "789",
"Name": "Tag789"
}
]
},
]
}
What is the correct syntax for an OData query that will return all records with any Tag/Ids that are contained in input list?
The closest I have got is:
Tags/any(object: object/Id search.in ('123,456,789'))

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);
}
}
}

Sugar CRM 6.5 API

I am new to the Sugar CRM 6.5 API. I am having trouble getting my url to post data to the leads module in Sugar. Here is the posting url,
https://yoursite/service/v4_1/rest.php?method=set_entry&input_type=JSON&response_type=JSON&rest_data={"name_value_list":{"last_name":"Peter","status":"New","email1":"test#test.com","lead_source":"test"},"module_name":"Leads"}
Here is my response,
{
"id": "459bf4a2-0b3b-5857-feb5-56fe81e621eb",
"entry_list": {
"modified_by_name": {
"name": "modified_by_name",
"value": "admin"
},
"id": {
"name": "id",
"value": "459bf4a2-0b3b-5857-feb5-56fe81e621eb"
},
"name": {
"name": "name",
"value": " "
},
"date_entered": {
"name": "date_entered",
"value": "2016-04-01 14:10:12"
},
"date_modified": {
"name": "date_modified",
"value": "2016-04-01 14:10:12"
},
"modified_user_id": {
"name": "modified_user_id",
"value": "1"
},
"created_by": {
"name": "created_by",
"value": "1"
},
"deleted": {
"name": "deleted",
"value": 0
},
"full_name": {
"name": "full_name",
"value": " "
},
"do_not_call": {
"name": "do_not_call",
"value": false
},
"converted": {
"name": "converted",
"value": false
},
"lead_source": {
"name": "lead_source",
"value": "Web Site"
},
"status": {
"name": "status",
"value": "New"
},
"jjwg_maps_lat_c": {
"name": "jjwg_maps_lat_c",
"value": 0
},
"jjwg_maps_lng_c": {
"name": "jjwg_maps_lng_c",
"value": 0
}
}
}
When I view the leads, I get a blank record created. Any idea's, Thanks
The format of name_value_list is as follow:
{
"name_value_list":
"full_name": {"name": "full_name", "value": "John"}
}

Multi-dimensional arrays in Mandrill with handlebars

I'm trying to loop through a multi-dimensional array to get properties of products that are part of line items. They look basically like this: (I did a json_encode so it would be easier to read)
[{
"rcpt": "email#email.com",
"vars": [{
"name": "SYSTEM",
"content": "Bikes"
}, {
"name": "CUSTOMERSERVICE",
"content": "(855-553-4889)"
}, {
"name": "IMAGE",
"content": "http:\/\/www.url.com\/assets\/images\/chicago\/email\/dear_member.jpg"
}, {
"name": "LINKCOLOR",
"content": "#3db7e4"
}, {
"name": "FACEBOOK",
"content": "Bikes"
}, {
"name": "TWITTER",
"content": "Bikes"
}, {
"name": "INSTAGRAM",
"content": "Bikes"
}, {
"name": "CLOSING",
"content": "Greetings"
}, {
"name": "item",
"content": [{
"lineItem": 1,
"id": "3",
"name": "24-Hour Pass Gift Certificate",
"quantity": 2,
"nameShort": "24-Hour",
"type": "Gift Certificate",
"image": "24hour_blank.jpg",
"price": "9.95",
"total": "19.90",
"taxable": false,
"giftCertificates": {
"3204": {
"id": "3204",
"redemptionNumber": "xxxxx",
"type": "24-Hour"
},
"3205": {
"id": "3205",
"redemptionNumber": "xxxxx",
"type": "24-Hour"
}
}
}, {
"lineItem": 2,
"id": "1",
"name": "Annual Membership Gift Certificate",
"quantity": 2,
"nameShort": "Annual",
"type": "Gift Certificate",
"image": "annual_blank.jpg",
"price": "75.00",
"total": "150.00",
"taxable": false,
"giftCertificates": {
"892": {
"id": "892",
"redemptionNumber": "xxxxxx",
"type": "Annual"
},
"893": {
"id": "893",
"redemptionNumber": "xxxxx",
"type": "Annual"
}
}
}]
}, {
"name": "orderID",
"content": 1220
}, {
"name": "giftMessage",
"content": false
}, {
"name": "email",
"content": "email#email.com"
}, {
"name": "transactionDate",
"content": "12\/23\/2015"
}, {
"name": "transactionTime",
"content": "12:21 pm"
}, {
"name": "salesTaxTotal",
"content": 0
}, {
"name": "salesTaxRatePercent",
"content": "6.250"
}, {
"name": "TransactionAmount",
"content": "169.90"
}, {
"name": "account_number",
"content": "XXXX1111"
}, {
"name": "card_type",
"content": "Visa"
}, {
"name": "firstName",
"content": "tetete"
}, {
"name": "lastName",
"content": "tethuhhu"
}, {
"name": "address",
"content": "295 Place St"
}, {
"name": "city",
"content": "Brooklyn"
}, {
"name": "state",
"content": "NY"
}, {
"name": "zip",
"content": "11238"
}, {
"name": "country",
"content": "US"
}, {
"name": "phone",
"content": "8888888888"
}, {
"name": "transactionId",
"content": "xxxxxx"
}, {
"name": "shipToFirstName",
"content": "tetete"
}, {
"name": "shipToLastName",
"content": "tethuhhu"
}, {
"name": "shipToAaddress",
"content": "295 Place St"
}, {
"name": "shipToCity",
"content": "Brooklyn"
}, {
"name": "shipToState",
"content": "NY"
}, {
"name": "shipToZipCode",
"content": "11238"
}, {
"name": "ShipToCountry",
"content": "US"
}, {
"name": "ShipToCountry",
"content": "US"
}]
}]
So I am trying to get a print out of each gift certificate's type and redemption number. When I iterate through {{ giftCertificates }} like this:
{{#each giftCertificates}}
{{type}} {{redemptionNumber}}
{{/each}}
I get one of the line items but not the other. I'm guessing maybe it is being overwritten when it loops through again? But I have also tried to loop through {{ item }} and grab {{ giftCertificates.type }} and {{ giftCertificates.redemptionNumber }} and that does not work either. What is the correct way to get all of these from each line item?
Thanks for your help.
I know this is a very old question, but:
you can use {{this.proprietyName}} to get the type and number:
{{#each giftCertificates}}
{{this.892.type}}
{{/each}}
do not forget to add this to the mandrill message o
"merge": true,
"merge_language": "handlebars",
Also, the data structure is not ideal:
giftCertificates[
{
"id": "892",
"redemptionNumber": "xxxxxx",
"type": "Annual"
},
{
"id": "893",
"redemptionNumber": "xxxxxx",
"type": "Annual"
}
]
would be easier to handle.

Resources