I have a dynamic array as input.
From this array I need to create another array.
For example I have an array like below :
var InputArr = [{
"IM_Id": "1",
"IM_Description": "XXX5",
"IM_FirstProcessDate": "10-19-2016",
"IM_Alias": "test",
"IM_MasterPrice": "$33.00",
"IM_ProductId": "22001204",
"IM_ProductDescription": "TEST EP PRODUCT FOR DEVELOPMENT",
"IM_EPProduct": "yes",
"ItemMasterNumber": "290015",
"FS_Id": "2002",
"FS_Code": "XXX5",
"FS_Name": "XXX5",
"FS_Description": "XXX5",
"FS_EnablementType": "",
"FS_EPProduct": "yes",
"CH_Id": "4787",
"CH_ChargeId": "23004746",
"CH_Name": "XXX5",
"CH_Description": "",
"CH_Type": "One Time",
"CH_EPProduct": "yes",
"CA_Id": "1628",
"CA_ListPrice": "99",
"CA_FairValueBasis": "BESP",
"CA_FairValueLow": "99",
"CA_FairValueHigh": "99",
"CA_EffectiveStartDate": "04-18-2017",
"CA_EPProduct": "yes",
"OF_Id": "3881",
"OF_OfferId": "20003880",
"OF_Name": "XXX5",
"OF_Description": "XXX5",
"OF_Level": "BASE",
"OF_Type": "19-APR-2017",
"OF_EffectiveStartDate": "04-18-2017",
"OF_EPProduct": "yes"
}]
I need to iterate through this array and push the values to a new array but dynamically.
I cant take the parameters again like pushing the values as
"IM_Id":val['IM_Id']
My code should dynamically consider the keys and their values. This is because the initial input array is dynamically created and might have some additional keys values too.
How should I push these values dynamically to resulting array?
I tried Something like
resultingArray.push({InputArrKey1 :
InputArrayValue2,InputArrKey2:InputArrayValue2});
Related
I have a form which has a repeatable field group. I am using serializeArray() on the form submit to get all of the form values however the repeatable fields aren't in an easily workable format so I need to modify these so I am able to utilise them within a foreach
This is the response from the serializeArray()
{
"_acffr_repeatable_group_fields": "[\"acrepeater-vehical[0][vehicle-make]\",\"acrepeater-vehical[0][vehicle-model]\",null,\"acrepeater-vehical[0][vehicle-year]\",\"acrepeater-vehical[1][vehicle-make]\",\"acrepeater-vehical[1][vehicle-model]\",null,\"acrepeater-vehical[1][vehicle-year]\",\"acrepeater-vehical[2][vehicle-make]\",\"acrepeater-vehical[2][vehicle-model]\",null,\"acrepeater-vehical[2][vehicle-year]\"]",
"_acffr_repeatable_groups": "[\"acrepeater-vehical\"]",
"your-name": "Name",
"your-email": "test#test.com",
"YourZipCode": "zip code",
"acrepeater-vehical[0][vehicle-make]": "Make 1",
"acrepeater-vehical[0][vehicle-model]": "Model 1",
"acrepeater-vehical[0][vehicle-type]": "Car",
"acrepeater-vehical[0][vehicle-year]": "2001",
"acrepeater-vehical[1][vehicle-make]": "Make 2",
"acrepeater-vehical[1][vehicle-model]": "Model 2",
"acrepeater-vehical[1][vehicle-type]": "Truck",
"acrepeater-vehical[1][vehicle-year]": "2002",
"acrepeater-vehical[2][vehicle-make]": "Make 3",
"acrepeater-vehical[2][vehicle-model]": "Model 3",
"acrepeater-vehical[2][vehicle-type]": "Commercial",
"acrepeater-vehical[2][vehicle-year]": "2003"
}
As you can see from the above, for each repeated group of fields it adds the [N] to the acpeater-vehical however this flat format doesn't permit me to loop through the fieldsets
How could I reformat this to be in the appropriate format?
How to pass array of values as filter in angularjs
My scenario
cuisines json: ["food", "Alcohol","Thai"]
catorigeres json :["home", "office","school"]
values with cuisines and categories :
[{
"_id": "1",
"businessTypeName": "Pizza Hut",
"cus_name": ["food","Thai"],
"cat_name": ["home", "school"],
}, {
"_id": "2",
"businessTypeName": "Chicken Hut",
"cus_name":["Alcohol","Thai"],
"cat_name": ["office", "home"],
}, {
"_id": "3",
"businessTypeName": "Fish Hut",
"bussiness_url": "/dist/images/loop_image_sample_3.png",
"cus_name": ["Thai"],
"cat_name": ["office", "school"],
}]
cuisines and categories are of in checkbox if i click anyone it will append the array of values to {{selected}}
my question is how to filter values in {{selected}} to listing_loop div
my plunker demo
I don't think you can do that in html alone.
You can add a filtering function to the scope:
$scope.customFilter = function(value, index, array) {
// make sure `value.id` is a string
return ["food","Thai","Alcohol"].indexOf(value.id);
}
and using like this in HTML
<div ng-repeat="set in get | filter:customFilter"></div>
For a Chrome app, wich stores data in IndexedDB, i have a object like this:
var simplifiedOrderObject = {
"ordernumber": "123-12345-234",
"name": "Mr. Sample",
"address": "Foostreet 12, 12345 Bar York",
"orderitems": [
{
"item": "brush",
"price": "2.00"
},
{
"item": "phone",
"price": "30.90"
}
],
"parcels": [
{
"service": "DHL",
"track": "12345"
},
{
"service": "UPS",
"track": "3254231514"
}
]
}
If i store the hole object in an objectStore, can i use an index for "track", which can be contained multiple times in each order object?
Or is it needed or possibly better/faster to split each object into multiple objectStores like know from relational DBs:
order
orderitem
parcel
The solution should also work in a fast way with 100.000 or more objects stored.
Answering my own question: I have made some tests now. It looks like it is not possible to do this with that object in only 1 objectStore.
An other example object which would work:
var myObject = {
"ordernumber": "123-12345-234",
"name": "Mr. Sample",
"shipping": {"method": "letter",
"company": "Deutsche Post AG" }
}
Creating an index will be done by:
objectStore.createIndex(objectIndexName, objectKeypath, optionalObjectParameters);
With setting objectKeypath it is possible to address a value in the main object like "name":
objectStore.createIndex("name", "name", {unique: false});
It would also be possible to address a value form a subobject of an object like "shipping.method":
objectStore.createIndex("shipping", "shipping.method", {unique: false});
BUT it is not possible to address values like the ones of "track", which are contained in objects, stored in an array. Even something like "parcels[0].track" to get the first value as index does not work.
Anyhow, it would be possible to index all simple elements of an array (but not objects).
So the following more simple structure would allow to create an index entry for each parcelnumber in the array "trackingNumbers":
var simplifiedOrderObject = {
"ordernumber": "123-12345-234",
"name": "Mr. Sample",
"address": "Foostreet 12, 12345 Bar York",
"orderitems": [
{
"item": "brush",
"price": "2.00"
},
{
"item": "phone",
"price": "30.90"
}
],
"trackingNumbers": ["12345", "3254231514"]
}
when creating the index with multiEntry set to true:
objectStore.createIndex("tracking", "trackingNumbers", {unique: false, multiEntry: true});
Anyhow, the missing of the possibility to index object values in arrays, makes using indexedDB really unneeded complicated. It's a failure in design. This forces the developer to do things like in relational DBs, while lacking all the possibilities of SQL. Really bad :(
I am iterating over this in my html file to create a dynamic table header.
I have a nested object
$scope.tasks = [{ "Number": 159232, "Title": "BUG", "Status": "pending", "Link": "www.google.com", "Card": "www.kanban.com", "Point": { "Value": 1, "IsTimeBased": true }, "ApprovalStatus":{ "CR": true , "BA": true } };
$scope.titles = Object.keys($scope.tasks[0]);
This iterates through all the non nested items but I cant figure out how to add the keys from the nested object.
I tried
$scope.titles.push(Object.keys($scope.tasks.ApprovalStatus[0]));
But nothing seems to be working I tried a splice in this fashion as well.
Instead of using .push use .concat.
$scope.titles = $scope.titles.concat(Object.keys($scope.tasks.ApprovalStatus[0]));
You can also do the concat for multiple args simultaneously.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I'm new to JSON, how can I get the variation_id from this JSON based on the attributes_pa_trainer. jQuery and/or vanilla js answers are both appreciated.
Here's the JSON.
[{
"variation_id": "2446",
"attributes": {
"attribute_pa_trainer": "angus"
},
"image_src": "",
"image_link": "",
"image_title": "",
"image_alt": "",
"price_html": "",
"availability_html": "",
"sku": "TEST-ANGUS",
"weight": " kg",
"dimensions": "",
"min_qty": 1,
"max_qty": 0,
"backorders_allowed": false,
"is_in_stock": true,
"is_downloadable": false,
"is_virtual": true,
"is_sold_individually": "no"
}, {
"variation_id": "2447",
"attributes": {
"attribute_pa_trainer": "anthony"
},
"image_src": "",
"image_link": "",
"image_title": "",
"image_alt": "",
"price_html": "",
"availability_html": "",
"sku": "TEST-ANTHONY",
"weight": " kg",
"dimensions": "",
"min_qty": 1,
"max_qty": 0,
"backorders_allowed": false,
"is_in_stock": true,
"is_downloadable": false,
"is_virtual": true,
"is_sold_individually": "no"
}]
I'm looking for a solution to complete a function like the below
// On button click
$('.purchase_btn').click(function() {
// Set trainer to the value of the button's data attribute
var trainer = $(this).data('trainer');
// variations_json is the full json
var variations_json = $('.variations_form').data('product_variations');
// Get the variation id based on the trainer
var variation_id = [some way of getting the variation ID from the above json];
// Set a hidden field to the variation id
$('#variation_id').val(variation_id);
});
Thanks in advance.
Use the grep method to filter the objects. Assuming that you find one, the result is an array with one item, so you get the variation id from that item:
var variation_id = $.grep(variations_json, function(o){
return o.attributes.attribute_pa_trainer == trainer;
})[0].variation_id;
so [{id:1, name:"test"}, {id:2, name:"test2"}] is an array of json objects. So, I am little bit confused with your question, are you trying to get variation_id from each json object in the array ? or you want to do a loop through the array and find one of the json object and get its variation_id ?
Guffa's solution would work, but you just need to be careful with the jquery function, if nothing found, then it will return an empty array, so if the array is empty, array[0] will return an error.
Another approach could be to just loop through the JSON with jQuery's each function. I assume grep does more or less the same internally. This would look somewhat like this:
$.each(variations_json, function() {
if(this.attributes.attribute_pa_trainer == trainer){
variation_id =this.variation_id;
}
});
The downside being, that the whole JSON will be traversed no matter what, i.e. it obviously doesn't break off, when the trainer is found.