2

I have an object that looks like this:

  var serviceData = {};

  serviceData.energy = {commission:15.00, retention:0.00, conversion:0.099, renewal: false, offset:0, rental: true}
  serviceData.homeMedia = {commission:20.00, retention:0.00, conversion:0.02, renewal: false, offset:0, rental: true}
  serviceData.removal = {commission:4.00, retention:0.00, conversion:0.05, renewal: false, offset:2, rental: true}
  serviceData.storage = {commission:6.00, retention:0.09, conversion:0.01, renewal: true, offset:2, rental: true}
  serviceData.homeContents = {commission:50.00, retention:0.55, conversion:0.40, renewal: true, offset:0, rental: false}

I want to loop over the serviceData and if an item's property rental is false I want to remove that item from serviceData without actually modifying the serviceData object. In other words, create a new object minus the 'removed' item.

How can I do this???

1
  • Did you try using delete ... delete serviceData.homeContents; Commented Dec 22, 2015 at 22:55

5 Answers 5

2

Just iterate the object keys and only set the ones you want in the new object:

var serviceData = {};

serviceData.energy = {commission:15.00, retention:0.00, conversion:0.099, renewal: false, offset:0, rental: true}
serviceData.homeMedia = {commission:20.00, retention:0.00, conversion:0.02, renewal: false, offset:0, rental: true}
serviceData.removal = {commission:4.00, retention:0.00, conversion:0.05, renewal: false, offset:2, rental: true}
serviceData.storage = {commission:6.00, retention:0.09, conversion:0.01, renewal: true, offset:2, rental: true}
serviceData.homeContents = {commission:50.00, retention:0.55, conversion:0.40, renewal: true, offset:0, rental: false}

var filtered = {};
for(var key in serviceData) {
  if(serviceData[key].rental) {
    filtered[key] = serviceData[key];
  }
}

document.write(JSON.stringify(filtered, '\n'));

Note that the new object points to the same items ("sub-objects"), it's a shallow copy.

Sign up to request clarification or add additional context in comments.

1 Comment

pretty much had this on my screen but couldn't quite get there. thanks for this
1

First Object.assign(...) to copy the whole object, then use a "filter" function to sort out entries based on your rule.

Or use a mapfunction to return values based on your rule, then use "filter" to sort out all null entries.

Just as a hint to have a starting point…

1 Comment

my initial thought was to filter but i don't know how to do that on an object literal
1

I like using lodash library for strange manipulations :)

var newObj = _.omit(serviceData, function(property) {
    return !property.rental;
});

Comments

0

You could use a for-in loop and ignore the key of the item with a false rental.

function copyData(obj, ignoreKey) {
  var copiedObj = {};
  for (var key in obj) {
    if (key === ignoreKey) {
      continue;
    }
    copiedObj[key] = obj[key];
  }
  return copiedObj;
}
var newObject = copyObj(serviceData, 'homeContents');

Comments

0

you can use jquery each method:

var res={};
    $.each(serviceData, function( index ) {
  if(serviceData[index].rental)
   res[index] = serviceData[index]
})

pen

4 Comments

Why would you use jQuery for this?
@JimboJonny with JQuery each method you can iterate over object properties as if it was an array ( the method corresponds to [].forEach() ), it's just a way of abstracting the for-in loop
@maioman - But there's zero need for an index here, so why not just use a for-in loop? Even when you have jQuery already in a project there's no sense in using it in places where it doesn't even provide an advantage. It's even significantly more verbose and complex to write, requiring callback anonymous functions and such.
@JimboJonny this is a simple case, but using a forEach mehod on an object is quite cool ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.