how to map an array in an object map to another array - javascript

i have an object
{
"parent_entity_id": 394,
"display_name": "Test POI",
"event_code_prefix": "TEST",
"address": null,
"logo_url": "https://storage.png",
"is_active": true,
"identifier": [
{
"model_id": 10,
"entity_id": 575,
"is_active": true,
"valid_value": [
{
"type": "MACID",
"value": "AC:23:3F:23:8D:A1"
}
],
"display_name": "Test BLE Beacon 1",
"is_mandatory": true,
"entity_type_key": "BTBACID"
},
{
"model_id": null,
"entity_id": 576,
"is_active": true,
"valid_value": [
{
"type": "GEOFENCE_CIRCLE",
"value": {
"latitude": 24.155678,
"longitude": 54.425175,
"radius_in_meters": 500
}
}
],
"display_name": "Test tower gate 1",
"is_mandatory": true,
"entity_type_key": "GIS"
}
]
}
I need to map the array valid values from the object to another array with condition.
if Ble the map to valid_value_BLE array and if GIS to valid_values_GIS

try this
arr1=Obj.identifier.map(z => {
if(z.entity_type_key=='GIS'){z['valid_value_GIS']=z.valid_value;
}else{z['valid_value_BLE']=z.valid_value;}
delete z.valid_value; return z;
});
https://codepen.io/vkv88/pen/LYVZdwz?editors=0010

Related

How to add "collection" field to metadata for Solana NFTs with Visual Studio Code

I'm trying to add the collection field (name and family) to my metadata using VSC. Not sure where to begin or how to do it. Any help appreciated. Thanks!
{
"name": "Solflare X NFT",
"symbol": "",
"description": "Celebratory Solflare NFT for the Solflare X launch",
"seller_fee_basis_points": 0,
"image": "https://www.arweave.net/abcd5678?ext=png",
"animation_url": "https://www.arweave.net/efgh1234?ext=mp4",
"external_url": "https://solflare.com",
"attributes": [
{ "trait_type": "web", "value": "yes" },
{ "trait_type": "mobile", "value": "yes" },
{ "trait_type": "extension", "value": "yes" }
],
"collection": { "name": "Solflare X NFT", "family": "Solflare" },
"properties": {
"files": [
{
"uri": "https://www.arweave.net/abcd5678?ext=png",
"type": "image/png"
},
{
"uri": "https://watch.videodelivery.net/9876jkl",
"type": "unknown",
"cdn": true
},
{ "uri": "https://www.arweave.net/efgh1234?ext=mp4", "type": "video/mp4" }
],
"category": "video",
"creators": [
{ "address": "SOLFLR15asd9d21325bsadythp547912501b", "share": 100 }
]
}
}

How can i sort array with objects which have property with another array of objects but not with ES6 [duplicate]

This question already has answers here:
Sort nested array of object in javascript
(4 answers)
Closed 3 years ago.
I have this kind of array with objects. In each object except properties i have another array with objects called "secondary_fields". I need to access the last element in the secondary_fields array (secondary_fields[3]) where i have value of when it is created and after that to sort all of the objects
by the date time based on the value inside for example "2020-01-13 17:42:51";
But not with ES6 because i am using old platform where the ES6 version of Java Script is not supported
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
You can make use of the array method's sort method and sort on display_value.
arr.sort(function(a, b) {
return new Date(a.secondary_fields[3].display_value) - new Date(b.secondary_fields[3].display_value)
})
Edit
Or I guess in your case,
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
arr.sort(function(a, b){
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
})
console.log(arr)

How to return a childobject from json with lodash?

Suppose I have the following json file and I would like to return the 'existing.primaryBundle.products' array preferrably with lodash:
{
"existing": {
"hasPlatinum": false,
"primaryBundle": {
"id": "2008",
"name": "TV - Entertainment, Sport",
"products": [
{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
},
{
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false,
"swappableProducts": [
{
"name": "Movies",
"id": "105",
"price": 2000,
"gifted": false
}
]
}
]
},
"extrasBundle": {
"id": "131",
"name": "Optional Extras - MUTV (Sports), LFCTV (Sports), Chelsea TV (Sports)",
"products": [
{
"name": "MUTV (Sports)",
"id": "665",
"price": 0,
"gifted": false
},
{
"name": "LFCTV (Sports)",
"id": "666",
"price": 0,
"gifted": false
},
{
"name": "Chelsea TV (Sports)",
"id": "667",
"price": 0,
"gifted": false
}
]
}
}
}
I have tried lodash and use this statement:
list2 = _.pick(existing,'primaryBundle.products')
But this returns an error and not the wanted result. How can I select this products array?
you could use _.get(nameOfObject, 'existing.primaryBundle.products') of course you would need to name your object like I've done below with sampleObject;
check out the lodash docs too
const sampleObject = {
"existing": {
"hasPlatinum": false,
"primaryBundle": {
"id": "2008",
"name": "TV - Entertainment, Sport",
"products": [{
"name": "Entertainment",
"id": "100",
"price": 2600,
"gifted": false
}, {
"name": "Sport",
"id": "107",
"price": 2500,
"gifted": false,
"swappableProducts": [{
"name": "Movies",
"id": "105",
"price": 2000,
"gifted": false
}]
}]
},
"extrasBundle": {
"id": "131",
"name": "Optional Extras - MUTV (Sports), LFCTV (Sports), Chelsea TV (Sports)",
"products": [{
"name": "MUTV (Sports)",
"id": "665",
"price": 0,
"gifted": false
}, {
"name": "LFCTV (Sports)",
"id": "666",
"price": 0,
"gifted": false
}, {
"name": "Chelsea TV (Sports)",
"id": "667",
"price": 0,
"gifted": false
}]
}
}
}
console.log(_.get(sampleObject, 'existing.primaryBundle.products'));
The main reason why it returns an error is because existing is not a global scoped object, you have to assign object to some variable like const obj = {...} and then pass the parameter to _pick method as obj.existing, yet I don't see a reason to use lodash in here, you can just reference the path to that object directly.

paring json array in php from api

I am getting an array of specs from flipkart's API.
I am able to get this response from that API:
{
"nextUrl": "https://affiliate-api.flipkart.net/1.0/affiliate/feeds/fnkygygma/category/reh/55ab6c2673a4773ffb8e4019.json?expiresAt=1452881213871&sig=1c4c5111b6b014a71a17b229e6df6afc",
"validTill": 1452881213871,
"productInfoList": [
{
"productBaseInfoV1": {
"productId": "TDHDMH5GRSPZ3DNM",
"title": "Newhide Designer",
"productDescription": "",
"imageUrls": {
"400x400": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-400x400-imadmh8zgxbrsgq6.jpeg",
"200x200": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-200x200-imadmh8zgxbrsgq6.jpeg",
"unknown": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-original-imadmh8zgxbrsgq6.jpeg",
"800x800": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-800x800-imadmh8zgxbrsgq6.jpeg"
},
"productFamily": null,
"maximumRetailPrice": {
"amount": 1145,
"currency": "INR"
},
"flipkartSellingPrice": {
"amount": 1145,
"currency": "INR"
},
"flipkartSpecialPrice": {
"amount": 1145,
"currency": "INR"
},
"productUrl": "http://dl.flipkart.com/dl/newhide-designer/p/itmdp2nunbzffwzr?pid=TDHDMH5GRSPZ3DNM&affid=keshav",
"productBrand": "Newhide",
"inStock": true,
"codAvailable": true,
"discountPercentage": 0,
"offers": [],
"categoryPath": "[[{\"node_id\":20001,\"node_name\":\"FLIPKART_TREE\"},{\"node_id\":21183,\"node_name\":\"Lifestyle\"},{\"node_id\":21499,\"node_name\":\"Leather \\u0026 Travel Accessories\"},{\"node_id\":21960,\"node_name\":\"Wallets \\u0026 Clutches\"},{\"node_id\":21274,\"node_name\":\"Wallets \\u0026 Card Wallets\"}]]",
"styleCode": null,
"attributes": {
"size": "",
"color": "Black",
"storage": "",
"sizeUnit": "",
"displaySize": ""
}
},
"productShippingInfoV1": {
"shippingCharges": {
"amount": 0,
"currency": "INR"
},
"sellerName": null,
"sellerAverageRating": null,
"sellerNoOfRatings": 0,
"sellerNoOfReviews": 0
},
"categorySpecificInfoV1": {
"keySpecs": [
"Passport Holder",
"Passport Wallet"
],
"detailedSpecs": [],
"specificationList": [
{
"key": "General",
"values": [
{
"key": "Type",
"value": [
"Passport Organizer"
]
},
{
"key": "Ideal For",
"value": [
"Men's, Women's"
]
},
{
"key": "Material",
"value": [
"Leather, Cloth"
]
},
{
"key": "Slot for Cards",
"value": [
"Yes"
]
},
{
"key": "Slot for Passport",
"value": [
"Yes"
]
},
{
"key": "Slot for Cheque Book",
"value": [
"Yes"
]
},
{
"key": "Style Code",
"value": [
"CDBP040739"
]
},
{
"key": "Color Code",
"value": [
"Black"
]
}
]
},
{
"key": "Warranty",
"values": [
{
"key": " ",
"value": [
"1 Year Newhide warranty"
]
}
]
}
],
"booksInfo": {
"language": null,
"binding": null,
"pages": null,
"publisher": null,
"year": 0,
"authors": []
},
"lifeStyleInfo": {
"sleeve": null,
"neck": null,
"idealFor": [
"Men's",
"Women's"
]
}
}
}
}
I and not able decode
"categorySpecificInfoV1": {
"keySpecs": [
"Passport Holder",
"Passport Wallet"
],
I have tried json_decode. This gives error
Warning: json_decode() expects parameter 1 to be string, array given.
$keySpecs=$product['categorySpecificInfoV1']['keySpecs'];
$ar = json_decode($keySpecs,true);
I am new to PHP, how can I print the keyspecs from keyspecs array?
The warning is pretty clear. You need to pass the entire string you obtained from Flipkart into the json_decode() function.
See Documentation
$ar = json_decode($flipkart_result, true);
print_r($ar); //to check the array.
print_r($ar['categorySpecificInfoV1']['keySpecs']);
This should do. Post more information if this does not help.

Knockout JS changing my viewmodel creation to use mapping pulgin

I am relatively new to Knockout.js and have a question regarding the creating of the view model.
What I am currently doing is:
viewModel = {
Menu: ko.observableArray(ko.utils.parseJson(data)),
editMenu: function (menu) {
ko.applyBindings(menu, document.getElementById("MenuCategories"));
$("#MenuCategories").bPopup();
}
There is more than one function, but trying to keep it short.
What I want to do is use the mapping plugin because my json data that I am using contains arrays of objects with each object containing an array and each object in that array also contains an array. What the JSON looks like:
[
{
"Id": 1,
"Name": "Test 5",
"Description": "Testing menu",
"BeveragesMenu": false,
"Active": true,
"Categories": [
{
"Id": 1,
"Name": "Test 1",
"Active": true,
"MenuItems": [
{
"Id": 1,
"Name": "Food",
"Description": "2 Eggs and Bread",
"Price": 50,
"DateBased": false,
"PriceOnRequest": false,
"DateFrom": null,
"DateTo": null,
"Active": true
}
]
},
{
"Id": 2,
"Name": "Test 2",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1004,
"Name": "test",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1005,
"Name": "Test 4",
"Active": true,
"MenuItems": [
]
}
]
},
{
"Id": 92,
"Name": "Test 4",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 1006,
"Name": "Test 1",
"Active": true,
"MenuItems": [
{
"Name": "Test",
"Description": "",
"Price": "",
"DateBased": false,
"PriceOnRequest": false
}
]
}
]
},
{
"Id": 93,
"Name": "Test 6",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
]
},
{
"Id": 94,
"Name": "Test 9",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
]
},
{
"Id": 95,
"Name": "Test 20]",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 4,
"Name": "Test 6666",
"Active": true,
"MenuItems": [
]
}
]
},
{
"Id": 96,
"Name": "Test 444",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 3,
"Name": "Test 555",
"Active": true,
"MenuItems": [
]
},
{
"Id": 5,
"Name": "Test 6666",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1003,
"Name": "test 777",
"Active": true,
"MenuItems": [
]
}
]
}
]
Sorry for the long JSON code paste.
So now I am running into problems when updating certain parts of the objects within arrays withing objects etc.
So I taught that the mapping plugin might solve my problem as it creates all arrays as observable and I am assuming that is my problem.
So I tried:
viewModel = {
Menu: ko.mapping.fromJSON(ko.utils.parseJson(data)),
//Menu: ko.observableArray(ko.utils.parseJson(data)),
editMenu: function (menu) {
ko.applyBindings(menu, document.getElementById("MenuCategories"));
$("#MenuCategories").bPopup();
},
But then none of my binding are working and I can't seem to find the cause, any advice or tips on this matter would be appreciated.
Here is a Fiddle of all the things I have tried. http://jsfiddle.net/armandvdwalt/pjJc2/3/
Thanks
Try this fiddle, I switched the order of the resources. You were referencing the mapping plugin before knockout which was generating a script error about not recognizing "ko." Also, you don't need to use ko.utils.parseJson when using fromJSON, as fromJSON expects JSON as an argument.

Categories