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.
Related
I have a JSon like this :
[
{
"order_number": "",
"products": [
{
"line_id": "" ,
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
"cod_armazem": "",
"gln": "",
"sku_ah": "",
"name": "",
"qty": "",
"_regular_price": "",
"_sale_price": "",
"line_total": "",
"desconto": "",
"pva": "",
"category": "",
"localidade": " ",
"codigo_ah": " ",
"Part of": ""
},
{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
}
],
{
"order_number": "",
"products": [
{
"line_id": 1,
"cod_anf": "",
"static_field_2": "",
I need to remove the "products" but keep it's content.
So the output should be like this:
Is it possible? I've tried and I cant make it work.
Thanks
You can use nested maps, spread operator, and flat function like below.
const json = [{
"order_number": "2",
"products": [{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
"cod_armazem": "",
"gln": "",
"sku_ah": "",
"name": "",
"qty": "",
"_regular_price": "",
"_sale_price": "",
"line_total": "",
"desconto": "",
"pva": "",
"category": "",
"localidade": " ",
"codigo_ah": " ",
"Part of": ""
},
{
"line_id": "",
"cod_anf": "",
"static_field_2": "",
"nome_armazem": "",
}
]
},
{
"order_number": "1",
"products": [{
"line_id": 1,
"cod_anf": "",
"static_field_2": "",
}
]
}
];
const flattened = json.map(r => [...r.products.map(q =>
({
order_number: r.order_number,
...q
})
)])
.flat(1);
console.log(flattened);
Try this, assign json to data
let obj = [];
data.forEach(order =>{
order.products.forEach(product => {
obj.push({
...product,
order_number: order.order_number
})
})
})
I have a flat array consisting of the following data
[{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
},
{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 3
}, {
"text": "",
"type": "multichoice",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": ""
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 3
}, {
"text": "",
"type": "passage",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": 5
}
]
But, I will like to group the array in the format below.
[{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
},
{
"text": "",
"type": "true_false",
"ans": {
"ans_1": true,
"ans_2": false
},
"correct_ans": "",
"reason": "",
"passage": ""
}, {
"type": "passage",
"passage": 3,
"children": [{
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}, {
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}]
}, {
"text": "",
"type": "multichoice",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": "",
"passage": ""
}, {
"passage": 5,
"type": "passage",
"children": [{
"text": "",
"ans": {
"ans_1": "",
"ans_2": ""
},
"correct_ans": [],
"reason": ""
}]
}
]
I have searched online but; couldn't get my desired result. I also cam across this Q/A Build tree array from flat array in javascript. I tried to tweak the answer but still couldn't get my desired result.
Based on the the object generated from this JSON data, how can I get only the z:row object values using javascript or jQuery? In other words, how can I filter object values from JSON?
{
"Response": {
"Request": {
"ConnectInfo": {
"USER_NAME": "",
"PASSWORD": "",
"PROJECT_ID": "",
"CONNECTION_NAME": "",
"APP_ID": "",
"CLIENT_IP": "",
"SITE": "",
"LANGUAGE": "",
"LANGUAGE_ID": "0"
},
"Method": {
"MethodID": "",
"Name": "",
"PARAMS": {
"PARAM": [{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
},
{
"#id": "",
"#value": ""
}
]
},
"QueryControl": {
"chunk_num": "",
"rev_control": "",
"max_rows": ""
}
}
},
"MethodResponse": {
"Name": "",
"MethodID": "",
"DeResult": {
"xml": {
"#xmlns:dt": "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882",
"s:Schema": {
"#id": "RowsetSchema",
"s:ElementType": {
"#content": "eltOnly",
"#name": "row",
"s:AttributeType": [{
"#name": "BACKGROUND",
"#rs:maydefer": "true",
"#rs:nullable": "true",
"#rs:number": "1",
"#rs:writeunknown": "true",
"s:datatype": {
"#dt:maxLength": "2147483647",
"#dt:type": "string",
"#rs:precision": "0"
}
],
"s:extends": {
"#types": "rs:rowbase"
},
"rs:data": {
"z:row": [{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
]
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": "",
"Trace": ""
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": ""
}
}
"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
"{
"Response": {
"Request": {
"ConnectInfo": {
"LANGUAGE_ID": "0"
},
"Method": {
"MethodID": "",
"Name": "",
"PARAMS": {
"PARAM": [{
"#id": "",
"#value": ""
}]
},
"QueryControl": {
"chunk_num": "",
}
}
},
"MethodResponse": {
"Name": "",
"MethodID": "",
"DeResult": {
"xml": {
"#xmlns:dt": "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882",
"s:Schema": {
"#id": "RowsetSchema",
"s:ElementType": {
"#content": "eltOnly",
"#name": "row",
"s:AttributeType": [{
"#name": "BACKGROUND",
"#rs:maydefer": "true",
"#rs:nullable": "true",
"#rs:number": "1",
"#rs:writeunknown": "true",
"s:datatype": {
"#dt:maxLength": "2147483647",
"#dt:type": "string",
"#rs:precision": "0"
}
],
"s:extends": {
"#types": "rs:rowbase"
},
"rs:data": {
"z:row": [{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
{
"#BACKGROUND": "",
"#DOC.APP_FORMAT_TYPE": "NO VALUE",
"#DOC.AUTHOR": "",
"#DOC.Access_UDF": "Public",
"#DOC.BOM_DEFINING": "Yes",
"#DOC.CAGE_CODE": "CAGE_CODE1",
},
]
},
"ResultCode": "Ok",
"ExtraResults": "",
"Messages": ""
}
}
Thanks for help.
I found answer like below way we can pass through each object.
var obj = data;
var json = JSON.parse(data)
json.Response.MethodResponse.DeResult.xml["rs:data"]
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!
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);
}
;
}
})
})