I want to flatten my json nested array object to flat array
Both key and value pair should be dynamic as per the user input array
I tried to code myself but I am not much familiar with JavaScript function to do concat, push or other funtions.
Input:
{
"ReportID": "ProfitAndLoss",
"ReportName": "Profit and Loss",
"ReportType": 7,
"ReportTitles": [
"Profit & Loss",
"Clearlight Saunas Australia Pty Ltd",
"1 May 2017 to 31 May 2017"
],
"ReportDate": "17 May 2017",
"UpdatedDateUTC": "2017-05-17T05:52:23.414Z",
"Attributes": null,
"Fields": [],
"Rows": [
{
"RowType": "Header",
"Cells": [
{
"Value": "",
"Attributes": null
},
{
"Value": "31 May 17",
"Attributes": null
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Section",
"Cells": null,
"Title": "Income",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Curve Dome Sauna",
"Attributes": [
{
"Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Id": "account"
}
]
},
{
"Value": "6725.46",
"Attributes": [
{
"Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Essential Sauna Sales",
"Attributes": [
{
"Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Id": "account"
}
]
},
{
"Value": "21359.09",
"Attributes": [
{
"Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Premier Sauna Sales",
"Attributes": [
{
"Value": "7f5bb82f-f488-441e-b578-c3e6d16829f0",
"Id": "account"
}
]
},
{
"Value": "19088.18",
"Attributes": [
{
"Value": "7f5bb82f-f488-441e-b578-c3e6d16829f0",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Revenue - Installation ",
"Attributes": [
{
"Value": "e4985303-4ce7-4162-9ed3-e9a7799b5584",
"Id": "account"
}
]
},
{
"Value": "636.36",
"Attributes": [
{
"Value": "e4985303-4ce7-4162-9ed3-e9a7799b5584",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sanctuary sauna sale",
"Attributes": [
{
"Value": "90bda37d-e484-494c-8ad5-dd012f6acb56",
"Id": "account"
}
]
},
{
"Value": "26244.55",
"Attributes": [
{
"Value": "90bda37d-e484-494c-8ad5-dd012f6acb56",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna cover sale",
"Attributes": [
{
"Value": "5a1aa34f-9acb-4154-8d4c-bb7e56a16cc8",
"Id": "account"
}
]
},
{
"Value": "727.27",
"Attributes": [
{
"Value": "5a1aa34f-9acb-4154-8d4c-bb7e56a16cc8",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Income",
"Attributes": null
},
{
"Value": "74780.91",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Gross Profit",
"Attributes": null
},
{
"Value": "74780.91",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "Plus Other Income",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna Sale to Clearlight NZ Ltd",
"Attributes": [
{
"Value": "12eca2ea-00ff-4f1a-9187-bd3faaf15eae",
"Id": "account"
}
]
},
{
"Value": "1309.09",
"Attributes": [
{
"Value": "12eca2ea-00ff-4f1a-9187-bd3faaf15eae",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Other Income",
"Attributes": null
},
{
"Value": "1309.09",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "Less Operating Expenses",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Bank Fees",
"Attributes": [
{
"Value": "1ee9c97a-5707-4289-9d40-fa25d2f087d3",
"Id": "account"
}
]
},
{
"Value": "503.54",
"Attributes": [
{
"Value": "1ee9c97a-5707-4289-9d40-fa25d2f087d3",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "Row",
"Cells": [
{
"Value": "Sauna Storage",
"Attributes": [
{
"Value": "f3813ca9-9a0f-4c31-8afb-a1f9af61ca9a",
"Id": "account"
}
]
},
{
"Value": "670.00",
"Attributes": [
{
"Value": "f3813ca9-9a0f-4c31-8afb-a1f9af61ca9a",
"Id": "account"
}
]
}
],
"Title": null,
"Rows": null
},
{
"RowType": "SummaryRow",
"Cells": [
{
"Value": "Total Operating Expenses",
"Attributes": null
},
{
"Value": "1173.54",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
},
{
"RowType": "Section",
"Cells": null,
"Title": "",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "Net Profit",
"Attributes": null
},
{
"Value": "74916.46",
"Attributes": null
}
],
"Title": null,
"Rows": null
}
]
}
]
}
This is the desired output i want:
[
{
"ReportID": "ProfitAndLoss",
"ReportName": "Profit and Loss",
"ReportType": "7",
"ReportTitles": "Profit & Loss",
"ReportDate": "17 May 2017",
"UpdatedDateUTC": "2017-05-17T05:52:23.414Z",
"Attributes": null,
"Rows__RowType": "Header",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": null,
"Rows__Title": null,
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "",
"Rows__Rows____Cells__Attributes____Id": "",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": null
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "Clearlight Saunas Australia Pty Ltd",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "31 May 17",
"Rows__Cells____Attributes": null,
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "",
"Rows__Rows____Cells__Attributes____Id": "",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "1 May 2017 to 31 May 2017",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "Section",
"Rows__Cells": null,
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "Income",
"Rows__Rows____RowType": "Row",
"Rows__Rows____Cells__Value": "Curve Dome Sauna",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": null,
"Rows__Rows____Rows": null,
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "6725.46",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "a76bacdc-a411-4924-9860-7c8b1bf36e5b",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "Row",
"Rows__Rows____Cells__Value": "Essential Sauna Sales",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": null,
"Rows__Rows____Rows": null,
"Rows__Rows": ""
},
{
"ReportID": "",
"ReportName": "",
"ReportType": "",
"ReportTitles": "",
"ReportDate": "",
"UpdatedDateUTC": "",
"Attributes": "",
"Rows__RowType": "",
"Rows__Cells": "",
"Rows__Cells____Value": "",
"Rows__Cells____Attributes": "",
"Rows__Title": "",
"Rows__Rows____RowType": "",
"Rows__Rows____Cells__Value": "21359.09",
"Rows__Rows____Cells__Attributes": "",
"Rows__Rows____Cells__Attributes____Value": "b2544842-1e51-4c08-80bc-7077f01a8657",
"Rows__Rows____Cells__Attributes____Id": "account",
"Rows__Rows____Title": "",
"Rows__Rows____Rows": "",
"Rows__Rows": ""
}]
You can store this json in a variable and make use of something like
JSON.parse(variable_name);
After this it's in the form of an array and you can easily loop through it
Hope this helps!
Related
I'm manipulating some javascript objects and I want to know if is there a more efficient and easy way to process my data.
I already do that, but I'm a beginner in js.
I have four objects with this structure: basically there is an array of blocks and any object has a different number of blocks. In every block, in the features attribute, I have another array with some features.
Then I have another object, and I have to remove from this object (I call it structure) blocks and features that are not present in my four initial object.
This is a sample product object
[
{
"ID": 16293,
"SortNo": "20",
"FeatureGroup": {
"ID": "148",
"Name": {
"Value": "Design",
"Language": "IT"
}
},
"Features": [
{
"Localized": 0,
"ID": "155744521",
"Type": "dropdown",
"Value": "Round",
"CategoryFeatureId": "85327",
"CategoryFeatureGroupID": "16293",
"SortNo": "155",
"PresentationValue": "Rotondo",
"RawValue": "Round",
"LocalValue": [],
"Description": "The external form",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "9397",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Forma",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155655523",
"Type": "multi_dropdown",
"Value": "White",
"CategoryFeatureId": "85298",
"CategoryFeatureGroupID": "16293",
"SortNo": "90",
"PresentationValue": "Bianco",
"RawValue": "White",
"LocalValue": [],
"Description": "The colour of the housing",
"Mandatory": "1",
"Searchable": "1",
"Feature": {
"ID": "10059",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Colore struttura",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155655525",
"Type": "multi_dropdown",
"Value": "White",
"CategoryFeatureId": "85301",
"CategoryFeatureGroupID": "16293",
"SortNo": "80",
"PresentationValue": "Bianco",
"RawValue": "White",
"LocalValue": [],
"Description": "The colour of the band",
"Mandatory": "1",
"Searchable": "1",
"Feature": {
"ID": "11025",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Colore cinturino",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "219617494",
"Type": "y_n",
"Value": "Y",
"CategoryFeatureId": "168947",
"CategoryFeatureGroupID": "16293",
"SortNo": "-6",
"PresentationValue": "Sì",
"RawValue": "Y",
"LocalValue": [],
"Description": "The product is protected from water",
"Mandatory": "0",
"Searchable": "0",
"Feature": {
"ID": "7509",
"Sign": "",
"Measure": {
"ID": "26",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Resistente all'acqua",
"Language": "IT"
}
}
}
]
},
{
"ID": 34567,
"SortNo": "20",
"FeatureGroup": {
"ID": "184",
"Name": {
"Value": "Prestazione",
"Language": "IT"
}
},
"Features": [
{
"Localized": 0,
"ID": "155744528",
"Type": "y_n",
"Value": "N",
"CategoryFeatureId": "94697",
"CategoryFeatureGroupID": "34567",
"SortNo": "800",
"PresentationValue": "No",
"RawValue": "N",
"LocalValue": [],
"Description": "La Frequenza modulare radio produce la miglior recezione di qualsiasi canale radio. Quando viene usato un auricolare, produce un effetto di suono da stereo r",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "2172",
"Sign": "",
"Measure": {
"ID": "26",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Radio FM",
"Language": "IT"
}
}
},
{
"Localized": 0,
"ID": "155744530",
"Type": "multi_dropdown",
"Value": "Not supported",
"CategoryFeatureId": "85357",
"CategoryFeatureGroupID": "34567",
"SortNo": "500",
"PresentationValue": "Non supportato",
"RawValue": "Not supported",
"LocalValue": [],
"Description": "Types of memory cards which can be used with this product.",
"Mandatory": "1",
"Searchable": "0",
"Feature": {
"ID": "730",
"Sign": "",
"Measure": {
"ID": "29",
"Sign": "",
"Signs": {
"ID": "",
"_": "",
"Language": "IT"
}
},
"Name": {
"Value": "Tipi schede di memoria",
"Language": "IT"
}
}
}
]
}
]
Here i loop my initial objects (this.compare_products) to extract, in two arrays (featureGroupIds - featureIds) the ID of my block and the CategoryFeatureId
let featureGroupIds = []
let featureIds = []
this.compare_products.forEach((object) => {
featureGroupIds = featureGroupIds.concat(FeaturesGroups.map(o => o.ID))
featureIds = featureIds.concat(FeaturesGroups.map(o => o.Features.map(o => o. CategoryFeatureId))).flat(2)
})
The two arrays, featureGroupIds and featureIds are now filled with every block ID and every CategoryFeatureId present in my four object.
Now I have to filter the object I call "structure" to remove the block and the features with an ID that is not present in my arrays.
This is my structure, and as you can see is similar.
[
{
"name": "Display",
"data": {
"id": 34566,
"category_id": 2647
},
"features": [
{
"name": "Tipo di display",
"data": {
"id": 85325,
"category_id": 2647,
"feature_id": 9104,
"category_feature_group_id": 34566,
"order": 10100140
}
},
{
"name": "Touch screen",
"data": {
"id": 85331,
"category_id": 2647,
"feature_id": 4963,
"category_feature_group_id": 34566,
"order": 10100129
}
},
{
"name": "Dimensioni schermo",
"data": {
"id": 158002,
"category_id": 2647,
"feature_id": 3544,
"category_feature_group_id": 34566,
"order": 100149
}
},
{
"name": "à di Pixel",
"data": {
"id": 85347,
"category_id": 2647,
"feature_id": 13246,
"category_feature_group_id": 34566,
"order": 100147
}
},
{
"name": "Tipo di vetro",
"data": {
"id": 94704,
"category_id": 2647,
"feature_id": 7610,
"category_feature_group_id": 34566,
"order": 100050
}
}
]
},
{
"name": "Altre caratteristiche",
"data": {
"id": 34569,
"category_id": 2647,
"feature_group_id": 146,
"name": null,
"order": 0
},
"features": [
{
"name": "inside",
"data": {
"id": 110410,
"category_id": 2647,
"feature_id": 18688,
"category_feature_group_id": 34569,
"order": 100000
}
}
]
}
]
Here is my function
structure = structure.filter(featureGroup => this.featureGroupIds.includes(featureGroup.data.id));
structure.map((object) => {
object.features.filter(feature => this.featureIds.includes(feature.data.feature_id))
})
this.featureIds and this.featureGroupIds are the array with the group IDS and with the feature IDS.
Is there a more efficient way to do this?
I have 2 nested array and I want to check if id's from the list1 and if there are same id in the list2 add that object of the list2 + tag and count from the list1 to a new array.New array has the tag,count and the list of details of id that is in the list1 and is same as id in the list2
Note: these 2 lists doesn't have the same size
Thank in advance for help
For example:
list1
const list1 = [
{
"id": [
"5cca1dbc-dd5c-498f-8f83-735062c05240",
"2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
"3128f36c-1c79-4301-b08f-e0182c256c03"
],
"tag": "tag1",
"count": {
"low": 53,
"high": 0
}
},
{
"id": [
"510019af-1728-4628-9019-343cd3c1b3e1",
"fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
],
"tag": "tag2",
"count": {
"low": 43,
"high": 0
}
}
]
list2
[
{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
]
new array
[
{
"tag": "tag1",
"count": {
"low": 53,
"high": 0
},
"details": [
{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
]
}
]
Iterate through list1, check if the id exists in list2 and if it does add it to a new array.
e.g.
var result = [];
for (let item of list1) {
let details = list2.filter(l2 => item.id.includes(l2.id));
if (details.length > 0) {
result.push({
tag: item.tag,
count: item.count,
details: details
});
}
}
If you want all items in list1 to show up regardless of the id existing in list2 you could use map and return a new object for each item in list1.
var result = list1.map(l1 => {
return {
tag: l1.tag,
count: l1.count,
details: list2.filter(l2 => l1.id.includes(l2.id))
};
});
const list1 = [{
"id": [
"5cca1dbc-dd5c-498f-8f83-735062c05240",
"2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
"3128f36c-1c79-4301-b08f-e0182c256c03"
],
"tag": "tag1",
"count": {
"low": 53,
"high": 0
}
},
{
"id": [
"510019af-1728-4628-9019-343cd3c1b3e1",
"fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
],
"tag": "tag2",
"count": {
"low": 43,
"high": 0
}
}
];
const list2 = [{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
];
var result1 = [];
for (let item of list1) {
let details = list2.filter(l2 => item.id.includes(l2.id));
if (details.length > 0) {
result1.push({
tag: item.tag,
count: item.count,
details: details
});
}
}
console.log(result1);
var result2 = list1.map(l1 => {
return {
tag: l1.tag,
count: l1.count,
details: list2.filter(l2 => l1.id.includes(l2.id))
};
});
console.log(result2);
You can use array.forEach,
First you need to take list1 and loop through each item, inside each item you have property called id, which is again a collection of array. you need to do a foreach with the id and check with the list2 and once the list2.id matches with the id of list 1 . it should push an object of your required output.
I hope the below code will solve the issue.
const list1 = [
{
"id": [
"5cca1dbc-dd5c-498f-8f83-735062c05240",
"2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
"3128f36c-1c79-4301-b08f-e0182c256c03"
],
"tag": "tag1",
"count": {
"low": 53,
"high": 0
}
},
{
"id": [
"510019af-1728-4628-9019-343cd3c1b3e1",
"fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
],
"tag": "tag2",
"count": {
"low": 43,
"high": 0
}
}
]
const list2 = [
{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
]
var arr = []
list1.forEach(l1I => {
l1I.id.forEach(eID => list2.forEach(l2I => {
if(l2I.id === eID){
var obj = {}
obj["details"] = l2I;
obj["tag"] = l1I.tag;
obj["count"] = l1I.count;
arr.push(obj);
}
}))
})
console.log("output", arr)
Thank you so much for your responses,
I could solve it that day by this:
const responseValueList = {};
const hashtagsList = [];
for (const responseRow of list1) {
const obj = {};
obj.hashtagName = responseRow.hashtag;
obj.hashtagCount = responseRow.count;
const rateableList = []
for (const responseSectionTwo of list2) {
if (responseRow.id.includes(responseSectionTwo.id)) {
rateableList.push(responseSectionTwo);
}
}
obj.hashtagItems = rateableList;
hashtagsList.push(obj);
}
Thanks again
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.
I use jQuery get a JSON string from a API. I want to display it and change some field and PUT back to the API. But it seems to hard to gather information from input box and put it into right field.
Here is the json structure:
{
"economicStatus": "",
"title1": "",
"migrantEducation": "",
"disability": {
"status": "",
"section504Status": "",
"primaryDisability": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"awaitingInitialIDEAEvaluation": ""
},
"giftedTalented": "",
"firstNationalEnrollment": "",
"alertMessageList": [
{
"message": "object",
"messageType": ""
}
],
"medicalAlertMessageList": [
{
"message": "",
"severity": ""
}
],
"projectedGraduationYear": "",
"onTimeGraduationYear": "",
"graduationDate": "",
"economicDisadvantage": "",
"otherIdList": [
{
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"name": {
"nameRole": "",
"actualNameOfRecord": {
"prefix": "",
"familyNameFirst": "",
"preferredFamilyNameFirst": "",
"givenName": "",
"middleName": "",
"familyName": "",
"preferredFamilyName": "",
"preferredGivenName": "",
"fullName": "",
"sortName": "",
"suffix": ""
}
},
"phoneNumberList": [
{
"number": "",
"extension": "",
"phoneNumberType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"listedStatus": ""
}
],
"emailList": [
{
"emailAddress": "",
"emailType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"localId": {
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
},
"externalId": {
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
},
"addressRefIdList": [
{
"value": "",
"addressRole": ""
}
],
"otherNameList": [
{
"nameRole": "",
"otherName": {
"prefix": "",
"familyNameFirst": "",
"preferredFamilyNameFirst": "",
"givenName": "",
"middleName": "",
"familyName": "",
"preferredFamilyName": "",
"preferredGivenName": "",
"fullName": "",
"sortName": "",
"suffix": ""
}
}
],
"demographics": {
"birthDateVerification": "",
"sexus": "",
"birthDate": "",
"subregionOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"stateProvinceOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"placeOfBirth": "",
"countryOfBirth": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"countryArrivalDate": "",
"citizenshipStatus": "",
"languageProficiency": "",
"dwellingArrangement": "",
"maritalStatus": "",
"ethnicityList": [
{
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
],
"languageList": [
{
"languageCode": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"languageType": "",
"dialect": ""
}
],
"countryOfResidency": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
},
"countryOfCitizenshipList": [
{
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
]
},
"electronicIdList": [
{
"idValue": "object",
"idType": {
"code": "",
"codesetName": "",
"item": "object",
"otherCodeList": [
{
"value": "",
"codesetName": ""
}
]
}
}
],
"refId": ""
}
And here is a sample data I get back from API, with some field are null:
{
"disability": null,
"economicStatus": null,
"migrantEducation": null,
"title1": null,
"alertMessageList": null,
"medicalAlertMessageList": null,
"projectedGraduationYear": null,
"onTimeGraduationYear": null,
"graduationDate": null,
"giftedTalented": null,
"economicDisadvantage": null,
"firstNationalEnrollment": null,
"otherIdList": [
{
"idType": {
"code": "AuthenticationUsername",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "ChK#username-60ty"
},
{
"idType": {
"code": "HMOF UserName",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "ChK#username-60ty.arda.stu"
},
{
"idType": {
"code": "Role",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.OtherId"
},
"idValue": "learner"
}
],
"addressRefIdList": null,
"name": null,
"localId": {
"idType": {
"code": "LASID",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.LocalId"
},
"idValue": "79800003270ABCKrtg12"
},
"externalId": {
"idType": {
"code": "SASID",
"item": null,
"otherCodeList": null,
"codesetName": "HMH.ExternalId"
},
"idValue": "NULL"
},
"electronicIdList": null,
"otherNameList": null,
"demographics": null,
"phoneNumberList": null,
"emailList": null,
"refId": "3329962f-18ab-415f-addb-132cbe613d68"
}
Is there a simple way to change the fields in a page using input box? I try to create as many input boxes as needed and name them and associate them with corresponding JSON field... But it seems to tedious and can not update array dynamically.
I have two arrays like this :
var friendemail = [ {
"data": {
"status": "OK",
"message": "User list fetched successfully",
"userList": [
{
"userId": 1,
"emailId": "abc.com",
"firstName": "Abc",
"lastName": "Xyz",
"nickName": "Nic",
"type": "USER",
"apiKey": "355901361"
},
{
"userId": 2,
"emailId": "babitadhami#g.com",
"firstName": "Babita",
"lastName": "Dhami",
"nickName": "bobby",
"type": "USER",
"apiKey": "333234567"
},
{
"userId": 3,
"emailId": "testuser#g.com",
"firstName": "Test_User",
"lastName": "Account",
"nickName": "Testi",
"type": "USER",
"apiKey": "1403626362113"
},
{
"userId": 4,
"emailId": "dhami#gmail.com",
"firstName": "dhami",
"lastName": "Dhami",
"nickName": "bobby",
"type": "DEVELOPER",
"apiKey": "222234567"
},
{
"userId": 5,
"emailId": "babita.dhami#g.com",
"firstName": "Babita",
"lastName": "Dhami",
"nickName": "bobby",
"type": "USER",
"apiKey": "1403709178117"
},
{
"userId": 6,
"emailId": "Chris#abc.com",
"firstName": "dhami",
"lastName": "dhami",
"nickName": "dhami",
"type": "DEVELOPER",
"apiKey": "333234567"
},
{
"userId": 7,
"emailId": "kevin.wei#qdevinc.com",
"firstName": "abc",
"lastName": "xyz",
"nickName": "none",
"type": "DEVELOPER",
"apiKey": "111234567"
},
{
"userId": 8,
"emailId": "dhamji#gmail.com",
"firstName": "Bab",
"lastName": "Dham",
"nickName": "bobby",
"type": "USER",
"apiKey": "1403790266057"
},
{
"userId": 9,
"emailId": "info#hemlockhills.ca",
"firstName": "Jenn",
"lastName": "Becker",
"nickName": "JennB",
"type": "USER",
"apiKey": "355901361"
},
{
"userId": 10,
"emailId": "babitadhami1#gmail.com",
"firstName": "Babita",
"lastName": "Dhami",
"nickName": "bobby",
"type": "USER",
"apiKey": "333234567"
},
{
"userId": 11,
"emailId": "b.dhami#g.com",
"firstName": "Babita",
"lastName": "Dhami",
"nickName": "bobby",
"type": "USER",
"apiKey": "333234567"
},
{
"userId": 12,
"emailId": "dhami.babita#g.com",
"firstName": "Babita",
"lastName": "Dhami",
"nickName": "bobby",
"type": "USER",
"apiKey": "333234567"
},
{
"userId": 13,
"emailId": "Francie#abc.com",
"firstName": "Francie",
"lastName": "Francie",
"nickName": "Francie",
"type": "USER",
"apiKey": "111234567"
},
{
"userId": 14,
"emailId": "Sam#abc.com",
"firstName": "Sam",
"lastName": "M",
"nickName": "S",
"type": "USER",
"apiKey": "111234567"
},
{
"userId": 15,
"emailId": "Sid#abc.com",
"firstName": "Sid",
"lastName": "B",
"nickName": "S",
"type": "USER",
"apiKey": "22234567"
},
{
"userId": 16,
"emailId": "tim#outlook.com",
"firstName": "tim",
"lastName": "tim",
"nickName": "tim",
"type": "USER",
"apiKey": "111234567"
},
{
"userId": 17,
"emailId": "racing#gmail.com",
"firstName": "racing",
"lastName": "racing",
"nickName": "Hollywood",
"type": "USER",
"apiKey": "222234567"
}
]
},
"status": 200,
"config": {
"method": "GET",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "",
"headers": {
"Accept": "application/json, text/plain, */*"
}
},
"statusText": "OK"
}]
and another is
var deviceContactEmail = [
{
"id": "1",
"rawId": "1",
"displayName": "kandwal",
"name": {
"formatted": "kandwal",
"givenName": "kandwal"
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "testuser#g.com",
"id": "6",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/1/photo",
"type": "url",
"id": "1",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "2",
"rawId": "2",
"displayName": "xyz",
"name": {
"formatted": "xyz ",
"givenName": "xyz"
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "Chris#abc.com",
"id": "12",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/2/photo",
"type": "url",
"id": "7",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "3",
"rawId": "3",
"displayName": "cdf",
"name": {
"formatted": "cdf",
"givenName": "cdf"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": [
{
"region": "Uk",
"id": "19",
"locality": "Dehra Dun",
"formatted": "dddd",
"type": "home",
"pref": false,
"country": "India"
}
],
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/3/photo",
"type": "url",
"id": "14",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "4",
"rawId": "4",
"displayName": "abcd",
"name": {
"formatted": "scd ",
"givenName": "scd"
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "dhami#gmail.com",
"id": "26",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/4/photo",
"type": "url",
"id": "21",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "5",
"rawId": "5",
"displayName": "hiteshbhattcse#gmail.com",
"name": {
"formatted": "hiteshbhattcse#gmail.com ",
"givenName": "hiteshbhattcse#gmail.com"
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "hiteshbhattcse#gmail.com",
"id": "33",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/5/photo",
"type": "url",
"id": "28",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "6",
"rawId": "6",
"displayName": "hitet#gmail.com",
"name": {
"formatted": "hitet#gmail.com ",
"givenName": "hitet#gmail.com"
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "hiteshbhatt#gmail.com",
"id": "40",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/6/photo",
"type": "url",
"id": "35",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "7",
"rawId": "7",
"displayName": null,
"name": {
"formatted": ""
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "mayank.th088#gmail.com",
"id": "46",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/7/photo",
"type": "url",
"id": "41",
"pref": false
}
],
"categories": null,
"urls": null
},
{
"id": "8",
"rawId": "8",
"displayName": null,
"name": {
"formatted": ""
},
"nickname": null,
"phoneNumbers": null,
"emails": [
{
"type": "other",
"value": "gcjoshi83#gmail.com",
"id": "53",
"pref": false
}
],
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": "",
"photos": [
{
"value": "content://com.android.contacts/contacts/8/photo",
"type": "url",
"id": "48",
"pref": false
}
],
"categories": null,
"urls": null
}]
I want to compare both of them for email id . and wanna get the common email id inside 1 array and rest in one array from deviceContactEmail.
I tried angular. each but not getting success.
var wUser = [];
var nonWUser = [];
angular.forEach(deviceContactEmail, function(phonevalue){
console.log(phonevalue);
angular.forEach(friendemail, function(value){
if (phonevalue.emails) {
if(phonevalue.emails[0].value === value.emailId){
console.log(phonevalue.emails[0].value);
wUser.push(phonevalue);
}else{
console.log("------------------------------------------------------------------------");
console.log(phonevalue.emails[0].value);
nonWUser.push(phonevalue);
};
}
})
})
Hi Try this I solved to remove the duplicates value in the array check it...
var wUser = [];
var nonWUser = [];
angular.forEach(deviceContactEmail, function(phonevalue){
console.log(phonevalue);
angular.forEach(friendemail[0].data.userList, function (value) {
if (phonevalue.emails) {
if(phonevalue.emails[0].value === value.emailId){
console.log(phonevalue.emails[0].value);
wUser.push(phonevalue);
}
else {
console.log("------------------------------------------------------------------------");
console.log(phonevalue.emails[0].value);
if ($scope.nonWUser.length == 0) {
nonWUser.push(phonevalue);
}
var filteredVal= $scope.nonWUser.filter(function (filterValue) {
return filterValue.emails[0].value == phonevalue.emails[0].value;
});
if (filteredVal.length == 0)
nonWUser.push(phonevalue);
}
}
});
});
check the edited file
It seem you are not pointing the inner loop at the right list. friendemail is a list but contains only one item. I assume the emailId you are testing for is buried inside the data which happens to be child of friendemail. Try the code below
angular.forEach(deviceContactEmail, function(phonevalue) {
console.log("................ ",phonevalue, " ", friendemail[0].data.userList.length);
angular.forEach(friendemail[0].data.userList, function(value) {
if (phonevalue.emails) {
if (phonevalue.emails[0].value === value.emailId) {
console.log(phonevalue.emails[0].value);
wUser.push(phonevalue);
} else {
console.log("------------------------------------------------------------------------");
console.log(phonevalue.emails[0].value);
nonWUser.push(phonevalue);
}
;
}
})
})