I use $.ajax to get a make a json-request of a php-file.
The JSON-string is NOT built by myself. It is an external API. So I can't chance it in any ways.
The jQuery goes through all 1st dimension entries.
For 'also_known_as' it does also check the 2nd dimension for multiple entries.
The Problem occurs if there isn't any entry in de 2nd dimension.
How can I check if there is at least one 2nd dimension-entry?
I tried
if(typeof results[i].also_known_as[0] === "undefined")
and
if($.type(results[i].also_known_as[0]) === "undefined")
(also without "")
Any ideas?
The JSON looks like this:
[
{
"plot": "Former cop ...",
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"rating": 7.3,
"language": [
"English",
"Portuguese",
"Spanish"
],
"rating_count": 181888,
"country": [
"USA"
],
"release_date": 20110429,
"title": "Fast Five",
"year": 2011,
"filming_locations": "Rice, California, USA",
"imdb_id": "tt1596343",
"directors": [
"Justin Lin"
],
"writers": [
"Chris Morgan",
"Gary Scott Thompson"
],
"actors": [
"Vin Diesel",
"Paul Walker",
"Jordana Brewster",
"Tyrese Gibson",
"Ludacris",
"Matt Schulze",
"Sung Kang",
"Gal Gadot",
"Tego Calderon",
"Don Omar",
"Joaquim de Almeida",
"Dwayne Johnson",
"Elsa Pataky",
"Michael Irby",
"Fernando Chien"
],
"plot_simple": "Dominic ...",
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA##._V1_SY317_CR0,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
},
"runtime": [
"130 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt1596343/",
"also_known_as": [
{
"country": "Argentina",
"title": "Rápidos y furiosos 5in control"
},
{
"country": "Australia",
"title": "Fast & Furious 5"
},
{
"remarks": [
"Bulgarian title"
],
"country": "Bulgaria",
"title": "Бързи и яростни 5: Удар в Рио"
}
]
},
{
"plot": "Former cop ...",
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"rating": 7.3,
"language": [
"English",
"Portuguese",
"Spanish"
],
"rating_count": 181888,
"country": [
"USA"
],
"release_date": 20110429,
"title": "Fast Five",
"year": 2011,
"filming_locations": "Rice, California, USA",
"imdb_id": "tt1596343",
"directors": [
"Justin Lin"
],
"writers": [
"Chris Morgan",
"Gary Scott Thompson"
],
"actors": [
"Vin Diesel",
"Paul Walker",
"Jordana Brewster",
"Tyrese Gibson",
"Ludacris",
"Matt Schulze",
"Sung Kang",
"Gal Gadot",
"Tego Calderon",
"Don Omar",
"Joaquim de Almeida",
"Dwayne Johnson",
"Elsa Pataky",
"Michael Irby",
"Fernando Chien"
],
"plot_simple": "Dominic ...",
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA##._V1_SY317_CR0,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
},
"runtime": [
"130 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt1596343/"
}
]
This is a snippet of my jQuery function:
$.each(results, function(i, item) {
var id = results[i].imdb_id;
var title = results[i].title;
var also_known_as = '';
var also_known_as_prio = ''; //1 = Switzerland, 2 = Germany, 3 = Austria ... Geringere Prio -> wichtiger -> Überschreibt weniger wichtige
$.each(results[i].also_known_as, function(ii, iitem) {
switch (results[i].also_known_as[ii].country) {
case 'Switzerland':
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 1;
break;
case 'Germany':
if (also_known_as_prio != 1) {
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 2;
}
break;
case 'Austria':
if (also_known_as_prio != 1 && also_known_as_prio != 2) {
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 3;
}
break;
}
});
});
I think you wanted typeof results[i].also_known_as === "undefined" (without the [0]). Currently the code is trying to look up an element in the array and then check whether that element is undefined; since the array itself is undefined, it errors while trying to look up the array element before it even gets round to checking whether the element is undefined.
Related
I have two separate json files that are the response provided from an online api service and will hold the informations about the last two football season of Italian Serie A.
I want to use the unique team id and the result of the match to create a dataset to use with brain.js, what's the best way to achive this?
I've tried to unify the files by copying the content of the second file into the first ove but I will then have two matches array and other duplied fields and I'm not sure on how to process data to ingest into brain.js
{
"filters": {
"season": 2021
},
"resultSet": {
"count": 380,
"first": "2021-08-21",
"last": "2022-05-22",
"played": 380
},
"competition": {
"id": 2019,
"name": "Serie A",
"code": "SA",
"type": "LEAGUE",
"emblem": "https://crests.football-data.org/SA.png"
},
"matches": [
{
"area": {
"id": 2114,
"name": "Italy",
"code": "ITA",
"flag": "https://crests.football-data.org/784.svg"
},
"competition": {
"id": 2019,
"name": "Serie A",
"code": "SA",
"type": "LEAGUE",
"emblem": "https://crests.football-data.org/SA.png"
},
"season": {
"id": 757,
"startDate": "2021-08-21",
"endDate": "2022-05-22",
"currentMatchday": 38,
"winner": null
},
"id": 333779,
"utcDate": "2021-08-21T16:30:00Z",
"status": "FINISHED",
"matchday": 1,
"stage": "REGULAR_SEASON",
"group": null,
"lastUpdated": "2022-06-25T08:20:15Z",
"homeTeam": {
"id": 108,
"name": "FC Internazionale Milano",
"shortName": "Inter",
"tla": "INT",
"crest": "https://crests.football-data.org/108.png"
},
"awayTeam": {
"id": 107,
"name": "Genoa CFC",
"shortName": "Genoa",
"tla": "GEN",
"crest": "https://crests.football-data.org/107.svg"
},
"score": {
"winner": "HOME_TEAM",
"duration": "REGULAR",
"fullTime": {
"home": 4,
"away": 0
},
"halfTime": {
"home": 2,
"away": 0
}
},
"odds": {
"msg": "Activate Odds-Package in User-Panel to retrieve odds."
},
"referees": [
{
"id": 11336,
"name": "Valerio Marini",
"type": "REFEREE",
"nationality": "Italy"
}
]
},{ ... } //
],
// here I will have the other season
"filters": {
"season": 2020
},
"resultSet": {
"count": 380,
"first": "2021-08-21",
"last": "2022-05-22",
"played": 380
},
"competition": {
"id": 2019,
"name": "Serie A",
"code": "SA",
"type": "LEAGUE",
"emblem": "https://crests.football-data.org/SA.png"
},
"matches": [
{...}]
}
At the momen I've used this code on the json I have created to have training data.
const dataset = Object.values(samples.matches).map( (match) => {
let result
switch( match.score.winner ) {
case 'HOME_TEAM':
result = 0
break;
case 'AWAY_TEAM':
result = 1
break;
case 'DRAW':
result = 2
break;
}
return {
homeTeam: match.homeTeam.id,
awayTeam: match.awayTeam.id,
matchResult: result
}
})
//console.log( dataset )
const trainingData = dataset.map( (data) => {
return {
input: [ data.homeTeam, data.awayTeam ],
output: [ data.matchResult ]
}
})
this is what the data I'm using with brain will look
{ homeTeam: 112, awayTeam: 1106, matchResult: 2 },
{ homeTeam: 472, awayTeam: 113, matchResult: 1 },
{ homeTeam: 584, awayTeam: 98, matchResult: 1 },
{ homeTeam: 99, awayTeam: 107, matchResult: 2 },
{ homeTeam: 471, awayTeam: 1106, matchResult: 0 },
{ homeTeam: 472, awayTeam: 488, matchResult: 0 },
where 0 is when home team win, 1 when away team win and 2 if draw, but I need to have a better solution for handle this case.
Backstory (skipable):
Hello, I'm a student at Lambda school. Recently we had a JS assessment test in order to progress to the next unit, but I wounded up flunking it twice. So now I have to repeat Unit 1. I'm here today in order to finally knock out that damn exam. I don't have a problem working with Arrays or Objects, what I do have a problem with is figuring out a way to solve issues when they are combined. It's like: "What the hell, do I use map or Object.Key?" Those types of problems appeared throughout the exam. It's fine to challenge students, but I never worked with both at the same time before. The approach was completely lost on me and before I knew it, my 3 hours were up two days straight.
What I hope you can provide:
I'm going to provide one problem and show you the work that I did. What I would like is for you to formulate pseudo code for me to work out that problem. I need to learn how to use Pseudo code if I want to be a good web engineer. Please do not solve the problem for me, all I ask is pseudo code. I'll try to work on the problem based on the pseudo code I feel is manageable for me to do. Thanks in Advance.
Problem and current Status:
/**
* ### Challenge `getSecondStarshipName`
* MVP Challenge 🤓
*
* #instructions
* Return second starship's name from `starships` property.
* If length is 0. Return 'none'
*/
function getSecondStarshipName(character) {
// TODO: Add your code here.
const checkProperty = character.hasOwnProperty("starships");
const checkArray = character.isArray("starships");
const secondShipLength = character.starships[1].length;
const secondShipName = character.starships[1].name;
if (checkProperty && checkArray) {
if (secondShipLength >= 2) {
return secondShipName;
} else {
return "none";
}
} else {
return "none";
}
}
Data
window.lukeSkywalker = require('./person-1.json')
window.leiaOrgana = require('./person-5.json')
window.obiWanKenobi = require('./person-10.json')
},{"./person-1.json":2,"./person-10.json":3,"./person-5.json":4}],2:[function(require,module,exports){
module.exports={
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"homeworld": "Tatooine",
"films": [
"A New Hope",
"The Empire Strikes Back",
"Return of the Jedi",
"Revenge of the Sith",
"The Force Awakens"
],
"species": [
"Human"
],
"vehicles": [
{
"name": "Snowspeeder",
"model": "t-47 airspeeder",
"manufacturer": "Incom corporation",
"cost_in_credits": null,
"length": "4.5",
"max_atmosphering_speed": "650",
"crew": 2,
"passengers": 0,
"cargo_capacity": "10"
},
{
"name": "Imperial Speeder Bike",
"model": "74-Z speeder bike",
"manufacturer": "Aratech Repulsor Company",
"cost_in_credits": 8000,
"length": "3",
"max_atmosphering_speed": "360",
"crew": 1,
"passengers": 1,
"cargo_capacity": "4"
}
],
"starships": [
{
"name": "X-wing",
"model": "T-65 X-wing",
"manufacturer": "Incom Corporation",
"cost_in_credits": 149999,
"length": "12.5",
"max_atmosphering_speed": "1050",
"crew": 1,
"passengers": 0,
"cargo_capacity": "110",
"consumables": "1 week",
"hyperdrive_rating": "1.0",
"MGLT": "100",
"starship_class": "Starfighter"
},
{
"name": "Imperial shuttle",
"model": "Lambda-class T-4a shuttle",
"manufacturer": "Sienar Fleet Systems",
"cost_in_credits": 240000,
"length": "20",
"max_atmosphering_speed": "850",
"crew": 6,
"passengers": 20,
"cargo_capacity": "80000",
"consumables": "2 months",
"hyperdrive_rating": "1.0",
"MGLT": "50",
"starship_class": "Armed government transport"
}
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.co/api/people/1/"
}
},{}],3:[function(require,module,exports){
module.exports={
"name": "Obi-Wan Kenobi",
"height": "182",
"mass": "77",
"hair_color": "auburn, white",
"skin_color": "fair",
"eye_color": "blue-gray",
"birth_year": "57BBY",
"gender": "male",
"homeworld": "Stewjon",
"films": [
"A New Hope",
"The Empire Strikes Back",
"Return of the Jedi",
"The Phantom Menace",
"Attack of the Clones",
"Revenge of the Sith"
],
"species": [
"Human"
],
"vehicles": [
{
"name": "Tribubble bongo",
"model": "Tribubble bongo",
"manufacturer": "Otoh Gunga Bongameken Cooperative",
"cost_in_credits": null,
"length": "15",
"max_atmosphering_speed": "85",
"crew": 1,
"passengers": 2,
"cargo_capacity": "1600"
}
],
"starships": [
{
"name": "Jedi starfighter",
"model": "Delta-7 Aethersprite-class interceptor",
"manufacturer": "Kuat Systems Engineering",
"cost_in_credits": 180000,
"length": "8",
"max_atmosphering_speed": "1150",
"crew": 1,
"passengers": 0,
"cargo_capacity": "60",
"consumables": "7 days"
},
{
"name": "Trade Federation cruiser",
"model": "Providence-class carrier/destroyer",
"manufacturer": "Rendili StarDrive, Free Dac Volunteers Engineering corps.",
"cost_in_credits": 125000000,
"length": "1088",
"max_atmosphering_speed": "1050",
"crew": 600,
"passengers": 48247,
"cargo_capacity": "50000000",
"consumables": "4 years"
},
{
"name": "Naboo star skiff",
"model": "J-type star skiff",
"manufacturer": "Theed Palace Space Vessel Engineering Corps/Nubia Star Drives, Incorporated",
"cost_in_credits": null,
"length": "29.2",
"max_atmosphering_speed": "1050",
"crew": 3,
"passengers": 3,
"cargo_capacity": null,
"consumables": null
},
{
"name": "Jedi Interceptor",
"model": "Eta-2 Actis-class light interceptor",
"manufacturer": "Kuat Systems Engineering",
"cost_in_credits": 320000,
"length": "5.47",
"max_atmosphering_speed": "1500",
"crew": 1,
"passengers": 0,
"cargo_capacity": "60",
"consumables": "2 days"
},
{
"name": "Belbullab-22 starfighter",
"model": "Belbullab-22 starfighter",
"manufacturer": "Feethan Ottraw Scalable Assemblies",
"cost_in_credits": 168000,
"length": "6.71",
"max_atmosphering_speed": "1100",
"crew": 1,
"passengers": 0,
"cargo_capacity": "140",
"consumables": "7 days"
}
],
"created": "2014-12-10T16:16:29.192000Z",
"edited": "2014-12-20T21:17:50.325000Z",
"url": "https://swapi.co/api/people/10/"
}
},{}],4:[function(require,module,exports){
module.exports={
"name": "Leia Organa",
"height": "150",
"mass": "49",
"hair_color": "brown",
"skin_color": "light",
"eye_color": "brown",
"birth_year": "19BBY",
"gender": "female",
"homeworld": "Alderaan",
"films": [
"A New Hope",
"The Empire Strikes Back",
"Return of the Jedi",
"Revenge of the Sith",
"The Force Awakens"
],
"species": [
"Human"
],
"vehicles": [
{
"name": "Imperial Speeder Bike",
"model": "74-Z speeder bike",
"manufacturer": "Aratech Repulsor Company",
"cost_in_credits": 8000,
"length": "3",
"max_atmosphering_speed": "360",
"crew": 1,
"passengers": 1,
"cargo_capacity": "4"
}
],
"starships": [],
"created": "2014-12-10T15:20:09.791000Z",
"edited": "2014-12-20T21:17:50.315000Z",
"url": "https://swapi.co/api/people/5/"
}
},{}]},{},[1]);
How I would approach the problem (Pseudo Code):
//Create a variable called starship2nd
//Assign starship2nd the 2nd starship of each character.
//Check the length of the 2nd starship of each character. [[I'm stuck on this phase.]]
//Return 'none' after the checker function iterates through the Object's array. [[[Not sure how to do this part either..]]
Failed Solutions (Are they repairable? O_O)
solution 1 (failed)
const starship2nd = character.starships[1].name
const shipLength = starship2nd.length
function checkLength () {
if (shipLength === 0) {
return 'none'
}
return checkLength()
Thank you for your time. ^_^
Here is some pseudo code for you
if (character has property 'starships' and it is an array) {
if (length of starships is at least 2) {
return name property of second starship
} else {
return none
}
} else {
return none
}
You might think that some of those checks are overkill and not necessary since you know your input, but it is always good practice to double check properties and lengths before you begin accessing and assuming.
I hope that helps you with reasoning about the problem.
I have following input json payload,
{
"Products": {
"Product": [
{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}
]
}
}
And now I need to transform it into the following JSON format.
[
{
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"ProductID": 458761,
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"ProductID": 458761,
"S1": "03",
"S2": "08",
"Year": 2008
}
]
Can someone please help me to write a JavaScript to achieve this task. Any help is really appreciated.
EDIT: You changed the question :(
Assuming your original json is stored in a variable named input. This can be done using this code:
var output = input.Products.Product;
ORIGINAL: You can do this using map:
var output = input.Products.Product.map(function(inObj) {
return {
"Designation": inObj.Designation,
"EntryDate": inObj.EntryDate,
"S1": inObj.S1,
"S2": inObj.S2,
"Year": inObj.Year
}
});
This will give you the output you want - an array of objects, with the ProductIDs removed. I'm a bit rusty when it comes to working with object references, but you could possibly shorten this using delete:
var output = input.Products.Product.map(function(inObj) {
var outObj = inObj;
delete outObj.ProductID;
return outObj;
});
This will change the original input values as well though, so i wouldn't recommend it unless you don't plan on using that data again.
var first = {
"Products": {
"Product": [
{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}
]
}
}
then:
var second = first.Products.Product;
To make it exactly like you want:
for(var i = 0; i<second.length; i++){
delete second[i].ProductID;
}
You can use this little function:
var a = {
"Products": {
"Product": [{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
}, {
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}]
}
};
function newJSON(array){
var b = array.Products.Product;
b.forEach(function(e){delete e.ProductID});
return JSON.stringify(b);
}
document.write(newJSON(a));
With underscore:
var result = _.map(data.Products.Products, (product) => {
return _.omit(product, 'ProductID');
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to go through each hotel key and go inside rates to retrieve some info, how could I get values of them inside this JSON on JavaScript:
[
{
"auditData": {
"processTime": "1545",
"timestamp": "2016-04-08 04:33:17.145",
"requestHost": "66.226.74.194",
"serverId": "sa37AUX3ROLBLIS.env",
"environment": "[int]",
"release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
"internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|1|3|N|||||||||1"
},
"hotel": {
"checkIn": "2016-04-11",
"checkOut": "2016-04-12",
"code": 87399,
"name": "Premier",
"categoryCode": "3EST",
"categoryName": "3 ESTRELLAS",
"destinationCode": "MDF",
"destinationName": "Ciudad de Mexico",
"zoneCode": 10,
"zoneName": "Downtown",
"latitude": "19.431353",
"longitude": "-99.156457",
"rooms": [
{
"code": "DBL.2D",
"name": "DOBLE DOS CAMAS DOBLES",
"rates": [
{
"rateKey": "20160411|20160412|W|71|87399|DBL.2D|CGW-TODOS1|BB||1~2~1|2|N#-644903865",
"rateClass": "NOR",
"rateType": "BOOKABLE",
"net": "687.31",
"discount": "111.89",
"discountPCT": "14.00",
"sellingRate": "799.20",
"rateComments": "Incluye desayuno americano para adulto y menor\nel hotel no cuenta con aire acondicionado ",
"paymentType": "AT_WEB",
"packaging": false,
"boardCode": "BB",
"boardName": "ALOJAMIENTO Y DESAYUNO",
"cancellationPolicies": [
{
"amount": "799.20",
"from": "2016-04-08T23:59:00-05:00"
}
],
"rateBreakDown": {
"rateDiscounts": [
{
"code": "DN",
"name": "Descuento Niño",
"amount": "-281.68"
}
]
},
"rooms": 1,
"adults": 2,
"children": 1,
"childrenAges": "2",
"offers": [
{
"code": "9001",
"name": "Descuento niños",
"amount": "-281.68"
}
]
}
]
}
],
"totalSellingRate": "799.20",
"totalNet": "687.31",
"currency": "MXN"
}
},
{
"auditData": {
"processTime": "1543",
"timestamp": "2016-04-08 04:33:19.469",
"requestHost": "66.226.74.194",
"serverId": "sa3RKSJACHXE79K.env",
"environment": "[int]",
"release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
"internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|3|3|N|||||||||1"
},
"hotel": {
"checkIn": "2016-04-11",
"checkOut": "2016-04-12",
"code": 87399,
"name": "Premier",
"categoryCode": "3EST",
"categoryName": "3 ESTRELLAS",
"destinationCode": "MDF",
"destinationName": "Ciudad de Mexico",
"zoneCode": 10,
"zoneName": "Downtown",
"latitude": "19.431353",
"longitude": "-99.156457",
"rooms": [
{
"code": "SGL.DB",
"name": "INDIVIDUAL CAMA DOBLE",
"rates": [
{
"rateKey": "20160411|20160412|W|71|87399|SGL.DB|CGW-TODOS1|BB||1~1~1|5|N#-644903865",
"rateClass": "NOR",
"rateType": "BOOKABLE",
"net": "687.31",
"discount": "111.89",
"discountPCT": "14.00",
"sellingRate": "799.20",
"rateComments": "Incluye desayuno americano para adulto y menor\nel hotel no cuenta con aire acondicionado ",
"paymentType": "AT_WEB",
"packaging": false,
"boardCode": "BB",
"boardName": "ALOJAMIENTO Y DESAYUNO",
"cancellationPolicies": [
{
"amount": "799.20",
"from": "2016-04-08T23:59:00-05:00"
}
],
"rateBreakDown": {
"rateDiscounts": [
{
"code": "DN",
"name": "Descuento Niño",
"amount": "-641.98"
}
]
},
"rooms": 1,
"adults": 1,
"children": 1,
"childrenAges": "5",
"offers": [
{
"code": "9001",
"name": "Descuento niños",
"amount": "-641.98"
}
]
}
]
}
],
"totalSellingRate": "799.20",
"totalNet": "687.31",
"currency": "MXN"
}
}
]
I was trying to use for (var key in data), where data is the JSON response from AJAX.
It's an array. You should iterate using a normal for loop, like
for(var i = 0; i < data.length;i++) {
var rooms = data[i].hotel.rooms
for(var j = 0; j < rooms.length; j++) {
var rates = rooms.rates .. //do something with rates, it's also an array
}
}
Use a each loop each time you see a [] the first character in the string is bracket so we do a each we select the child in our case "hotel" using dot notation v.hotel,v.hotel doesn't start with a bracket so we don't need a loop,we go down in the json object until we reach the rates key where we see another braket so we loop and select the child element in our case the rateKey
var obj =[
{
"auditData": {
"processTime": "1545",
"timestamp": "2016-04-08 04:33:17.145",
"requestHost": "66.226.74.194",
"serverId": "sa37AUX3ROLBLIS.env",
"environment": "[int]",
"release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
"internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|1|3|N|||||||||1"
},...];
$.each(obj, function(i, v) {
$.each(v.hotel.rooms, function(i1, v1) {
$.each(v1.rates, function(i2, v2) {
console.log(v2.rateKey);
});
})
});
https://jsfiddle.net/78cpwq5g/
Use the Json object.
If suppose the values are stored in JsonObject.
And some values you may want to retrieve.
Eg:- "code": 87399,
"name": "Premier",
"categoryCode": "3EST",
"categoryName": "3 ESTRELLAS",
"destinationCode": "MDF",
"destinationName": "Ciudad de Mexico",
$(JsonObject).each(function(){
$(this).hotel.code;
$(this).hotel.name;
})
I'm trying to merge two javascript objects that has inside of it arrays and other elements. Let me give you an example.
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai1#gmail.com"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839332"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "johndoemae1#gmail.com"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": "johndoealuno1#gmail.com"
}
],
"class": [
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
},
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": false,
"see_all": true
}
As you can see, we can have arrays inside with some strings in those arrays.
I've tried to do it using some recursion, but had no success.
function mergeObjects(target, source){
var item, tItem, o, idx;
if (typeof source == 'undefined') {
return source;
} else if (typeof target == 'undefined') {
return target;
}
for (var prop in source) {
item = source[prop];
//check if the first element is indeed an object.
if (typeof item == 'object' && item !== null) {
//if the first item is an array
if (_.isArray(item) && item.length) {
//dealing with array of primitives
if (typeof item[0] != 'object') {
item.forEach(function(conteudo){
//push to the target all the elements;
target[prop].push(conteudo);
});
}else{
//dealing with array of objects;
for(var attr in item){
idx = {};
tItem = target[attr]
mergeObjects(tItem,item);
}
}
}//if its a normal object
else {
// deal with object
mergeObjects(target[prop],item);
}
} else {
// item is a primitive, just copy it over
target[prop] = item;
}
}
return target;
}
I Expected this:
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6",
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
}
"fullname": "John Doe 1",
"eid": "1234",
"classes": [
"Sala 1",
"Sala 2",
"Sala 3",
"Sala 4",
"Sala 5",
"Sala 6"
],
"addresses": [{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839332"
}, {
"type": "email",
"tags": [
"Mãe"
],
"address": "johndoemae1#gmail.com"
}, {
"type": "email",
"tags": [
"Aluno"
],
"address": "johndoealuno1#gmail.com"
}, {
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
}, {
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
}, {
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
}, {
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
}],
"invisible": true,
"see_all": true
}
But what i got was this, as you can see, some email elements are missing.
{
"addresses": [
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai2#gmail.com"
},
{
"type": "email",
"tags": [
"Responsável",
"Pai"
],
"address": "johndoepai3#gmail.com"
},
{
"type": "phone",
"tags": [
"Pai"
],
"address": "5519985504400"
},
{
"type": "phone",
"tags": [
"Responsável",
"Mãe"
],
"address": "551138839333"
},
{
"type": "email",
"tags": [
"Mãe"
],
"address": "11 983340440"
},
{
"type": "email",
"tags": [
"Aluno"
],
"address": ""
}
],
"class": [
"Sala 4",
"Sala 5",
"Sala 6",
"Sala 1",
"Sala 2",
"Sala 3"
],
"fullname": "John Doe 1",
"eid": "2",
"invisible": true,
"see_all": false
}
Order of elements is not a problem.
Which point of the recursion tree did i miss?
Short answer: you have to merge the arrays yourself first. Or you can use something nice like Lodash's mergeWidth Ramda's R.mergeWith (see below for sample code for Ramda).
You want something like the native Object.assign (limited browser support, so it will have to be polyfilled) or whatever merge your JS library has.
The problem you're running into is how all of these merge implementations handle duplicate keys. Usually the key will be set to the last value passed in:
var thing1 = {
someProperty: 'foo'
};
var thing2 = {
someProperty: 'bar'
};
var result1 = Object.assign({}, thing1, thing2);
// -> {someProperty: 'bar'} (set to the last value passed in, in thing2)
var result2 = Object.assign({}, thing2, thing1);
// -> {someProperty: 'foo'} (again set to the last value passed in, in thing1)
// Make the Stackoverflow snippet display the output.
document.body.innerHTML += JSON.stringify(result1, null, 2);
document.body.innerHTML += '<br>' + JSON.stringify(result2, null, 2);
Works the same way with your example, except the property is a more complicated array. It's just being set to the last array passed in:
var thing1 = {
someList: [1, 2, 3]
};
var thing2 = {
someList: [4, 5, 6]
};
var result = Object.assign({}, thing1, thing2);
// -> {someList: [4, 5, 6]}
// Make the Stackoverflow snippet display the output.
document.body.innerHTML = JSON.stringify(result, null, 2);
If you use something nice like Ramda, you can merge and specify a function to control the behavior when keys are equal, like in your case. Here we can check if the value is an array, then concatenate if so. Otherwise, return the latest value (as described above):
var thing1 = {someList: [1, 2, 3]};
var thing2 = {someList: [4, 5, 6]};
var dupeMergeFn = function(a, b) {
return (Array.isArray(a)) ? a.concat(b) : b;
};
var result = R.mergeWith(dupeMergeFn, thing1, thing2);
// Make the Stackoverflow snippet display the output.
document.body.innerHTML = JSON.stringify(result, null, 2);
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.19.1/ramda.min.js"></script>