If-statement not filtering out undefined - javascript
I am trying to cleanse a dataset, but my if statement appears not to work.
The first part, x.sites.length>0, seems to work by itself.
The second part, (x !== undefined), doesn't seem to register at all. In my return Array, some of the undefineds are still there. I have tried various different ways of writing (x !== undefined), but none of them seem to work for me.
Any thoughts?
export function clenseData(dataset){
let cleansedSet = dataset.map(x=>{
if(x.sites.length>0 && (x !== undefined))
{return x;}//doesn't work.
})
console.log(cleansedSet);
debugger;
return cleansedSet;
}
Here is the JSON String for one of the inputs: {"data":{"success":true,"data":[{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Roxanne Modafferi","Valentina Shevchenko"],"commence_time":1555754400,"home_team":"Roxanne Modafferi","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[3.35,1.3]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[3.8,1.27]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[3.35,1.3]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[3.75,1.28]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[3.25,1.33]}}],"sites_count":5},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Marcin Tybura","Shamil Abdurakhimov"],"commence_time":1555754400,"home_team":"Marcin Tybura","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.57,2.3]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[1.63,2.39]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.55,2.4]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.57,2.3]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.6,2.31]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[1.53,2.38]}}],"sites_count":6},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Aleksei Oleinik","Alistair Overeem"],"commence_time":1555754400,"home_team":"Alistair Overeem","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.9,1.4]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.9,1.4]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.96,1.41]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.9,1.41]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[2.9,1.4]}}],"sites_count":5},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Keita Nakamura","Sultan Aliev"],"commence_time":1555754400,"home_team":"Sultan Aliev","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.8,1.95]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[1.85,1.97]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.81,2]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.8,1.95]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.86,1.92]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[1.8,1.91]}}],"sites_count":6},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alexander Volkov","Luis Henrique Da Silva"],"commence_time":1555786800,"home_team":"Luis Henrique Da Silva","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.74,2.05]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.66,2.23]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.66,2.18]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Marcin Tybura","Shamil Abdurakhimov"],"commence_time":1555790400,"home_team":"Shamil Abdurakhimov","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.55,2.4]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.53,2.48]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Keita Nakamura","Sultan Aliev"],"commence_time":1555790400,"home_team":"Sultan Aliev","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.8,2]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.79,1.99]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Roxanne Modafferi","Valentina Shevchenko"],"commence_time":1555790400,"home_team":"Roxanne Modafferi","sites":[{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[3.78,1.3]}},{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[3.7,1.27]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[3.45,1.28]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Aleksei Oleinik","Alistair Overeem"],"commence_time":1555801200,"home_team":"Aleksei Oleinik","sites":[{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[3.05,1.45]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[2.95,1.42]}},{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[3,1.39]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alex Oliveira","Mike Perry"],"commence_time":1556406000,"home_team":"Mike Perry","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Cory Sandhagen","John Lineker"],"commence_time":1556406000,"home_team":"Cory Sandhagen","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.15,1.65]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.15,1.65]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jack Hermansson","Ronaldo Souza"],"commence_time":1556413200,"home_team":"Jack Hermansson","sites":[{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.15,1.7]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.25,1.62]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.25,1.62]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Glover Teixeira","Ion Cutelaba"],"commence_time":1556413200,"home_team":"Glover Teixeira","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.2,1.62]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.24,1.65]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.31,1.63]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.2,1.62]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Court McGee","Dhiego Lima"],"commence_time":1556413200,"home_team":"Court McGee","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.62,2.2]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.6,2.33]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.62,2.2]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Andrei Arlovski","Leonardo Augusto Guimaraes"],"commence_time":1556413200,"home_team":"Andrei Arlovski","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.1,1.68]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.22,1.66]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.1,1.68]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Penne","Jodie Esquibel"],"commence_time":1556413200,"home_team":"Jessica Penne","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.55,2.35]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.51,2.55]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.55,2.35]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jason Gonzalez","Jim Miller"],"commence_time":1556413200,"home_team":"Jim Miller","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.25,1.6]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.27,1.63]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.25,1.6]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alex Volkanovski","Jose Aldo"],"commence_time":1557622800,"home_team":"Jose Aldo","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.05,1.72]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.05,1.72]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.09,1.74]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.12,1.74]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Andrade","Rose Namajunas"],"commence_time":1557626400,"home_team":"Rose Namajunas","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.65,2.2]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.65,2.2]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.71,2.13]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.7,2.18]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alexander Gustafsson","Ashlee Evans Smith"],"commence_time":1559422800,"home_team":"Alexander Gustafsson","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.44,2.7]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.48,2.6]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Eye","Valentina Shevchenko"],"commence_time":1560045600,"home_team":"Valentina Shevchenko","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[6,1.12]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[6,1.12]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[6.25,1.12]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[8,1.08]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Henry Cejudo","Marlon Moraes"],"commence_time":1560049200,"home_team":"Henry Cejudo","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.1,1.72]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.1,1.72]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.12,1.74]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.09,1.74]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Amanda Nunes","Holly Holm"],"commence_time":1562464800,"home_team":"Amanda Nunes","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.3,3.45]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.3,3.45]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.3,3.5]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.31,3.52]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jon Jones","Thiago Santos"],"commence_time":1562468400,"home_team":"Jon Jones","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.14,5.5]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.14,5.5]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.17,5]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.16,5.4]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Colby Covington","Kamaru Usman"],"commence_time":1562475600,"home_team":"Colby Covington","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Dustin Poirier","Khabib Nurmagomedov"],"commence_time":1568523600,"home_team":"Dustin Poirier","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Israel Adesanya","Robert Whittaker"],"commence_time":1569697200,"home_team":"Israel Adesanya","sites":[],"sites_count":0}]},"status":200,"statusText":"","headers":{"x-requests-remaining":"24","content-type":"application/json;charset=utf-8","x-requests-used":"126"},"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*"},"method":"get","url":"https://api.the-odds-api.com/v3/odds/?apiKey=b80b98e306cbe877f35c7db3804d2d72&sport=mma_mixed_martial_arts®ion=uk&mkt=h2h"},"request":{}}
Are you trying to filter the data? if so you should be using filter function & not map. The map is used when you want to iterate over the data.
export function clenseData(dataset){
let cleansedSet = dataset.filter(x => {
if(x !== undefined && x.sites.length > 0) {
return true; // if you want to include it in the cleaned data
}
})
return cleansedSet;
}
The every() method tests whether all elements in the array pass the test implemented by the provided function:
dataset.every((x) => x !== undefined && x.sites.length> 0);
Related
Difference between if (array.length > 0) and if (array?.length) in Javascript (Why is array.length > 0 falsey?)
I'm a super green developer that started my first job last week. I got a comment from another developer to add a condition to check if array is undefined and to check if array is in fact an array. Initially I used: $: hasActiveCoupons = $cart.coupons && $cart.coupons.length > 0; if (hasActiveCoupons && $cart.coupons.find((coupon) => coupon.id === couponCode)) { ... } But then she pointed out to me that $cart.coupons.length > 0 would return a falsey value. And corrected it by writing: if ($cart.coupons?.length && $cart.coupons.find((coupon) => coupon.id === couponCode)) { ... } So I am wondering how this works? I tired reading a few other questions but I don't fully grasp this concept.
$cart.coupons.length > 0 will return a falsey value if coupons is empty, but there is no need to check length this way in this case. A smaller approach to this flow would be the following: $cart.coupons?.find(({ id }) => id === couponCode); If coupons is undefined, undefined is returned. If no values satisfy the testing function inside find, undefined is returned. Therefore, no need to test length separately.
Skipping undefined elements from an API call
I am gathering data from an API to show the Pokemon typing. Some Pokemon have one type, while others have two. The variables to gather the typing data are as such. function createPokemonCard(pokemon) { const type = pokemon.types[0].type.name; const second_type = pokemon.types[1].type.name; ...} And then I call them via InnerHTML in the same function with the following code. <small class="type"><span>${type}/${second_type}</span></small> Predictably, when it hits undefined for a Pokemon, it breaks and doesn't display the card. However I am not sure how to get it to not print the second type when it's undefined. I thought about doing an if statement, but what should I call if there is an undefined variable? function undefined { if(second_type === 'undefined') { ??? }} Or would a for loop work better? I am not sure how to get it to bypass the undefined and print nothing.
const second_type = pokemon.types[1] ? pokemon.types[1].type.name: undefined; `<small class="type"><span>${type}${second_type!=undefined ? `/${second_type}`: ''}</span></small>` The ? : syntax is a ternary operator (mdn) It's a less verbose way of writing out the following: if (second_type!=undefined) { // check if second_type is not undefined return `/${second_type}` // if it's not return / and the second type } else { //otherwise return '' // return an empty string }
If you do not want to display the trailing / when second_type is not defined one way to go could be const type = pokemon.types.map(({ type }) => type.name).join("/") and then <small class="type"><span>${type}</span></small>
Want to shorten my if statement using Lodash
I am looking to optimize the below piece of code using Lodash. I have looked for solutions online, but could not find anything relevant. Can someone please help? if (isPage && isPageType === constants.keys.isDisclosureSubmitted) { if (form && form.application && typeof form.application.disclosureAccepted !== 'undefined' && form.application.disclosureAccepted !== null && !form.application.disclosureAccepted) { return $q.when(response.data.content); } }
if (isPage && isPageType === constants.keys.isDisclosureSubmitted) { const discAcc = _.get(form, ['application', 'disclosureAccepted'], true); if (!_.isNil(discAcc) && !disAcc) { return $q.when(response.data.content); } } Basically, with _.get, you don't have to worry about checking a property of undefined, since it will just return undefined in that scenario rather than throw an error. _.isNil check is the value is both null or undefined, and then you want to make sure the value is still falsey. I mean, I'm not sure if lodash "optimizes" in this case, since these function calls will actually slow it down, but it is slightly more readable. Like the comments, I'm not sure exactly why you need to be so specific; you could just use _.get(form, ['application', 'disclosureAccepted']) == false, thought I sway against using unstrict equivalence.
React.js – filter multiple indexOf
Im just looking for a bit of advice regarding React.js filtering. I am currently filtering ‘peopleList’ by ‘employeeName’ and this is working fine, im getting back exactly what I expect. But I wanted to also filter by the ‘employeeID’ at the same time i.e. check if ‘employeeName’ or ‘employeeID’ contain an indexOf.. Is this possible or would I need to set up two filters for 'employeeName’ and 'employeeID’? let people= this.state.peopleList.filter( (person) => { return person.record.employeeName.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1; // return person.record.employeeID.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1; } );
If your condition is either one OR the other, you can use the || operator const { search, peopleList } = this.state const searchStr = search.toLowerCase() const people = peopleList.filter((person) => person.record.employeeName.toLowerCase().indexOf(searchStr) !== -1 || person.record.employeeId.indexOf(searchStr) !== -1 )
Compare Objects in Array and Remove Duplicate & Update - Javascript
I have an array of objects that presents as follows: 0: Object ConsolidatedItem_catalogId: "080808" ConsolidatedItem_catalogItem: "undefined" ConsolidatedItem_cost: "0" ConsolidatedItem_description: "Test Catalog Item" ConsolidatedItem_imageFile: "27617647008728.jpg" ConsolidatedItem_itemNumber: "1234" ConsolidatedItem_quantity: "1" ConsolidatedItem_source: "CAT" ConsolidatedItem_status: "02" ConsolidatedItem_umCode: "EA" 1: Object ConsolidatedItem_catalogId: "" ConsolidatedItem_catalogItem: "undefined" ConsolidatedItem_cost: "0" ConsolidatedItem_description: "ALARM,SHUTDOWN SYSTEM,AXIOM,XP3, 0-1500 PSIG, HIGH AND LOW PRES Testing" ConsolidatedItem_imageFile: "" ConsolidatedItem_itemNumber: "10008" ConsolidatedItem_quantity: "1" ConsolidatedItem_source: "INV" ConsolidatedItem_status: "02" ConsolidatedItem_umCode: "EA" I'm trying to update and remove an object if it's added again, or update the object. Preferably update the object with the new value. My code is as follows: var result = $.grep(finalObject, function(e) { return e.ConsolidatedItem_itemNumber == o.ConsolidatedItem_itemNumber; }); console.log(result); if (result.length == 0) { finalObject.push(o); shoppingCounter = finalObject.length; $('#numberShoppedItems').text(shoppingCounter); console.log(finalObject); } else if (result.length == 1) { finalObject.filter(function(x){ result = x; console.log(result); return x == result.ConsolidatedItem_itemNumber; }); } else { alert('Multiples Found'); } } I've tried multiple ways of getting the exact object and manipulating the data, however they've all failed. I would prefer to update the object, say if CatalogItem_itemNumber held the same value, if the CatalogItem_quantity was different - add the CatalogItem_quantity values together and update the array of objects. I don't need an exact answer, a nudge in the right direction would do wonders though. I've looked at several of the related questions over the past couple of hours but none of them seem to address the issue. If you know of a question that has an answer, feel free to just link that as well. I may have missed it. No Underscore.js please
When you find the matching record, you may update it by using $.extend $.extend(result[0], o) This will update the object in finalObject array in-place. Alternatively, if you want to use the filter, you will need to insert the new object in the array. finalObject = finalObject.filter(function(x) { return x !== result[0]; }); finalObject.push(o) Here we are allowing all the records that are not not equal to result to be returned in the resultant array that is received in finalObject. In next line, we are adding the new record.
Solved in the following manner: 1.) Verify object is not empty. 2.) Use .some() on object to iterate through it. 3.) Check if the finalObject, which is now e, has a match for the key in my temporary object I assemble, o. 4.) Update the values that need updating and return true; Note: Originally I was going to remove the object by its index and replace it with a new object. This too can work by using .splice() and getting the index of the current object in that array you're in. Here is the updating version: if (o.ConsolidatedItem_quantity != '') { var result = $.grep(finalObject, function(e) { return e.ConsolidatedItem_itemNumber == o.ConsolidatedItem_itemNumber; }); if (result.length == 0) {...} else { finalObject.some(function (e) { if(e.ConsolidatedItem_itemNumber == o.ConsolidatedItem_itemNumber){ var a; a = +e.ConsolidatedItem_quantity + +o.ConsolidatedItem_quantity; e.ConsolidatedItem_quantity = a.toString(); document.getElementById(o.ConsolidatedItem_itemNumber).value=a; return true; }; }); } }