Handling key value pair in response - javascript

I have the following response.
response: {
"json": {
"response":{
"servicetype":"",
"functiontype":"",
"statuscode":"0",
"statusmessage":"Success",
"data":
[
{
"graphtype":"headcountgraph",
"xlabel":"state",
"ylabel":"count",
"s1":"contact",
"s2":"User",
"data":
[
"Total Contacts: 1 Users: 36",
[
{
"x":"India",
"s1":"3",
"s2":"3",
"lat":"22",
"long":"77"
}
]
]
}
]
}
}
};
I need to Assign $scope.state = India,
How to fetch that ? I have tried giving $scope.state = json.response.data[0].data[1][0].x, which returns undefined data.
The thing is it has key and entry. I really lacking to fetch entry values from the code.
I gave code this way
$.each(data, function(key, entry) { x.push(parseInt(entry.x)});
but it gives me values [india] in my console.

Try giving $scope.state = response.json.response.data[0].data[1][0].x ;
Your statement doesn't start from the root response object.

You were very close.
json.response.data[0].data[1]
returns the following object:
{"x":"India","s1":"3","s2":"3","lat":"22","long":"77"}
You can filter by the x key with:
json.response.data[0].data.filter(o => o[0].x == "India")[0];

Related

How to loop through JSON and add values to object

So I am having trouble figuring out how to create an array with two objects, looping through my object and adding some values to those objects in Javascript. Currently I have the following mock response:
const mockResponse =
{
"errors": [
{
"errorKey": "ERROR_NO_DELIVERY_OPTIONS",
"errorParameters": [
{
"errorMessage": "ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW",
"partNumbers": [
19308033,
19114798
]
},
{
"errorMessage": "Ship to Home not available for these orderItemId",
"orderItemIds": [
10315031,
10315032
],
"availableShipModeId": 13203
},
{
"errorMessage": "Pickup At Seller not available for these orderItemIds",
"orderItemIds": [
10222222,
10333333
],
"availableShipModeId": 13203
}
],
"errorMessage": "ERROR_NO_DELIVERY_OPTIONS",
"errorCode": "ERROR_NO_DELIVERY_OPTIONS"
}
]
}
I would like to have an array with two objects. One for the first error message("Ship to home...") and another for the second error message("Pickup at Seller..."). I would like to then loop through the JSON and add each "orderItemIds" to there respective object. For example, 10315031,10315032 would go to the first object and 10222222, 10333333 to the second.
You can use reduce to loop through your errors and use the errorMessage property as a key
const result = mockResponse.errors[0].errorParameters.reduce((prev, item) => {
const { errorMessage, orderItemIds } = item;
if (prev[errorMessage]) {
prev[errorMessage] = [...prev[errorMessage], ...orderItemsIds];
} else {
prev[errorMessage] = orderItemIds
}
return prev
}, {})
Let me know if this does answer your question

Loop to get value based on key name

Below is the JSON response I am getting. From this JSON response, I want to get the value of "fees" based on "detailComponent" with "FULL_FEE" only. But, somehow it gets the last value of "detailComponent" with "SUB_FEE" or others which is not correct.
I am sure not how to make this for loop condition to fix my issue. Can help to guide pls?
let data = {
"status": "SUCCESS",
"result": {
"originData": {
"detailType": "MSG",
"origin": [
{
"details": [
{
"detailComponent": "FULL_FEE",
"fees": 13564.00
},
{
"detailComponent": "SUB_FEE",
"fees": 8207.60
}
]
}
]
}
}
}
var getData = data.result.originData.origin[0].details[0].detailComponent;
console.log('getData: ', getData);
You can convert the array into a dictionary by the key (detailComponent) and the number (fees).
// Create Array
var items = [];
// Loop Through It
data.result.originData.origin[0].details.forEach((el, index) => items[el.detailComponent] = el.fees);
// Test
console.log(items["FULL_FEE"]);

Filter Array Using Partial String Match in Javascript

I have an array of objects where the value I need to filter on is buried in a long string. Array looks like:
{
"data": {
"value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
"partnerId": 1
}
},
So if I wanted to grab all the partnerId objects where value includes parent_sku how would I do that?
console.log(data.value.includes('parent_sku') returns cannot read property 'includes' of null.
EDIT:
Didn't think this mattered, but judging by responses, seems it does. Here's the full response object:
Response body: {
"data": {
"configurationByCode": [
{
"data": {
"value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
"partnerId": 1
}
}
I'm passing that into a re-usable function for filtering arrays:
const parentSkuPartners = filterArray(res.body.data.configurationByCode, 'parent_sku');
Function:
function filterArray(array, filterList) {
const newList = [];
for (let i = 0; i < array.length; i += 1) {
console.log('LOG', array[i].data.value.includes('parent_sku');
}
}
The problem is somewhere else. The code you've tried should work to find if a value contains a string – I've added it the snippet below and you'll see it works.
The issue is how you are accessing data and data.value. The error message clearly states that it believes that data.value is null. We would need to see the code around it to be able to figure out what the problem is. Try just logging to console the value of data before you run the includes function.
const data = {
"value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}", "partnerId": 1
};
console.log('includes?', data.value.includes('parent_sku'));
You can use data.value.includes('parent_sku') as you have suggested. The issue here is that your object is nested inside an unnamed object.
try:
"data": {
"value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
"partnerId": 1
}
The problem was some of the values for value were null. Adding an extra conditional fixed it:
if (array[i].data.value !== null) {
Use lodash includes, and lodash filter like
let configurationByCode = [{
data: {
value: {
cols:["parent_sku"],
label:"Style",
description:"Enter Style.",
placeholderText:"Style 10110120103"
},
"partnerId": 1
}
}, {
data: {
value: {
cols:["nothing"],
label:"Style",
description:"Enter Style.",
placeholderText:"Style 10110120103"
},
"partnerId": 2
}
}];
let wantedData = _.filter(configurationByCode, (config) => {
return _.includes(config.data.value.cols, 'parent_sku');
});
console.log( wantedData );
https://jsfiddle.net/76cndsp2/

Ajax array transforming into associative array

So, I'm building an API in Ruby on Rails and I'm trying to force an array to be sent through ajax.
It seems quite an easy task if it wasn't for the fact that the array is being received as an associative array other than regular array!
Basically an array with objects like:
[
{
"shipping_id":"1",
"option":"1"
},
{
"shipping_id":"2",
"option":"2"
}
]
becomes:
{"0"=>{"shipment_id"=>"1", "option"=>"1"}, "1"=>{"shipment_id"=>"2", "option"=>"2"}}
instead of
[{"shipping_id"=>"1", "option"=>"1"}, {"shipping_id"=>"2", "option"=>"2"}]
This is the JS I'm using to test the API:
function select_shipping(){
obj1 = {
"shipment_id": "1",
"option": "1"
};
obj2 = {
"shipment_id": "2",
"option": "2"
};
var shipments = [obj1, obj2];
var payload = {
user_options: shipments
}
$.post('/shipping/calculate/select', // url
payload, // data to be submit
function(data, status, jqXHR) {// success callback
console.log(data);
})
}
How can I transform my payload to go as a regular array instead of associative?
You can do the following:
Note that this solution can process any number of shipments.
var shipments = [
{
"shipping_id":"1",
"option":"1"
},
{
"shipping_id":"2",
"option":"2"
}
]
var payload = {}
shipments.map(
function(shipment, index){
payload["$".concat(index)] = shipment;
}
);
console.log(payload);

How to check if a value exists in an JSON key that has an array of values

say I have the following JSON:
{
"selfExclusionMessage":{
"accountId":989898,
"expired":"false",
"userId":"37327513",
"products": [
"arcade", "vegas", "ex"
]
}
}
How can I check that a specific value is present in the products key array. For example if "arcade" value is present there. I would like to have an if statement that checks values in products array and do different things for each of the values ( arcade, vegas, ex), sometimes all three of the values will be present and sometimes only 1 or 2.
You just have to parse it with JSON.parse. The you can use includes array prototype to check if the value is in the array.
const data = JSON.parse(json)
const { products } = data.selfExclusionMessage
if (products.includes('arcade')) {
// do your stuff here
}
You can use conditional (ternary) operator with includes():
var jsonData = {
"selfExclusionMessage":{
"accountId":989898,
"expired":"false",
"userId":"37327513",
"products": [
"arcade", "vegas", "ex"
]
}
}
function isExists(data, key, value){
return jsonData.selfExclusionMessage[key].includes(value)? 'Exists' : 'Does not exists';
}
console.log(isExists(jsonData,'products','arcade'));
console.log(isExists(jsonData,'products','arcadexyzzzzzz'));
How about
let o = JSON.parse(myJSON);
for (let v of Object.values(o)) {
if (Array.isArray(v)) return Array.includes("arcade");
else return false;
}
Explanation: (1) Parse JSON into an object (2) check all object values for arrays (3) if there's an array, check to see if it has the desired value
var JSON = {
"selfExclusionMessage": {
"accountId": 989898,
"expired": "false",
"userId": "37327513",
"products": [
"arcade", "vegas", "ex"
]
}
};
if ("products" in JSON.selfExclusionMessage) {
console.log('Exist!');
if (JSON.selfExclusionMessage.products.includes("vegas")) {
console.log("includes");
} else {
console.log("!includes");
}
} else {
console.log('!Exist');
}
Note:- Simple way to solve this issue is to identify that object exists, then object key exists, then you check array includes value by includes method.
function searchValue( json, value ) {
return Array.isArray(json.selfExclusionMessage.products) && json.selfExclusionMessage.products.includes( value );
}

Categories

Resources