By myhouse


2018-07-11 23:03:03 8 Comments

I am trying to divide a JavaScript object into two separate arrays based in the fact if the value contact is null. I am having trouble storing the values into separate objects and I am not really sure how to store them in the respective object. I made a fiddle to help you play with this code.

var data = [{
    address: "234 BLUE WAY",
    addressType: "Mailing",
    city: "DONKY KONG CITY",
    contact: null,
    country: "US",
    email: "[email protected]",
    zipCode: "44222"
  },
  {
    address: "123 GETYO WAY",
    addressType: "Mailing",
    city: "NARNIA",
    contact: "JAMES DO",
    country: "US",
    email: "[email protected]",
    zipCode: "13123"
  }

];

var empContactInfoAddress;
var emergencyContactInfoAddress;

for (let d in data) {
  if (data[d].contact == null) {

    Object.keys(data[d]).forEach((key) => {
      this.empContactInfoAddress[d][key] = data[d][key];
    });
  } else {
    Object.keys(data[d]).forEach((key) => {
      this.emergencyContactInfoAddress[d][key] = data[d][key];
    });

  }
}
console.log('empContactInfoAddress', this.empContactInfoAddress)
console.log('emergencyContactInfoAddress', this.emergencyContactInfoAddress)

4 comments

@Aluan Haddad 2018-07-12 00:22:01

You could write a partition function

function partition(array, predicate) {
  const positives = array.filter(predicate);
  const negatives = array.filter(value => !predicate(value));
  return [positives, negatives];
}

const [
  empContactInfoAddress,
  emergencyContactInfoAddress
] = partition(data, d => !d.contact);

@Naga Sai A 2018-07-11 23:15:06

To achieve expected result, use below option of using if condition and pushing to variables if contact is null

var data = [
    {
        address: "234 BLUE WAY",
        addressType: "Mailing",
        city: "DONKY KONG CITY",
        contact: null,
        country: "US",
        email: "[email protected]",
        zipCode: "44222"
    },
    {
        address: "123 GETYO WAY",
        addressType: "Mailing",
        city: "NARNIA",
        contact: "JAMES DO",
        country: "US",
        email: "[email protected]",
        zipCode: "13123"
    }

];

var empContactInfoAddress =[];
var emergencyContactInfoAddress = [];

for (let d in data) {
   if(data[d].contact === null){
      empContactInfoAddress.push(data[d])
   }else{
     emergencyContactInfoAddress.push(data[d])
   }
}
console.log('empContactInfoAddress', empContactInfoAddress)
console.log('emergencyContactInfoAddress', emergencyContactInfoAddress)

@myhouse 2018-07-11 23:18:37

Thanks. I actually realize my mistake shortly after I post this. It's been a long day. :)

@MajiD 2018-07-11 23:12:06

var data = [{
    address: "234 BLUE WAY",
    addressType: "Mailing",
    city: "DONKY KONG CITY",
    contact: null,
    country: "US",
    email: "[email protected]",
    zipCode: "44222"
  },
  {
    address: "123 GETYO WAY",
    addressType: "Mailing",
    city: "NARNIA",
    contact: "JAMES DO",
    country: "US",
    email: "[email protected]",
    zipCode: "13123"
  }

];

data.forEach(function(arrayItem, i) {
  var x = arrayItem;
  console.log(x);
});

@Mark Meyer 2018-07-11 23:08:28

You can filter() based on the contact property.

If you have really large lists you can use something like reduce which would let you do this with one one loop through the data. But filter() is very easy to read, which for me balances the extra work unless there's a bottleneck.

var data = [{address: "234 BLUE WAY",addressType: "Mailing",city: "DONKY KONG CITY",contact: null,country: "US",email: "[email protected]",zipCode: "44222"},{address: "123 GETYO WAY",addressType: "Mailing",city: "NARNIA",contact: "JAMES DO",country: "US",email: "[email protected]",zipCode: "13123"}];

let NoContact = data.filter(d =>  d.contact == undefined)
let Contact = data.filter(d => d.contact)

console.log("no contact: ", NoContact)
console.log("contact: ", Contact)

@myhouse 2018-07-11 23:11:37

I just realize I was making this way more complicated and I never heard of filter. I am definitely going to use filter. Awesome! I have to wait a few minutes to accept this,

@Mark Meyer 2018-07-11 23:12:48

Yes @myhouse, filter, reduce and map are all wonderful tools to have.

Related Questions

Sponsored Content

69 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5227688 View
  • 6532 Score
  • 69 Answer
  • Tags:   javascript arrays

25 Answered Questions

37 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

64 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

35 Answered Questions

[SOLVED] Length of a JavaScript object

53 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

88 Answered Questions

[SOLVED] How do JavaScript closures work?

58 Answered Questions

[SOLVED] How can I merge properties of two JavaScript objects dynamically?

48 Answered Questions

Sponsored Content