0

I am really struggling to get to the names in this javascript object. Please could someone help me? What I want is to get someones name and if their "attending' status is true push their name into the "going" array and if their status is false push their name into the "not going" array.

The object is below and then the code I have written so far is below that. I don't know what to do when I get to the name level as they're different names...

"HmS7XXPFoCQ7GmvfcCnF": {
            "invitees": {
                "gus": {
                    "attending": true,
                    "invitedBy": "will"
                },
                "margot": {
                    "attending": "false",
                    "invitedBy": "gus"
                }
            }
        }

here is my code:

        Object.keys(invitees).map(function(keyName, i) {
            if (invitees[keyName] === true) {
                //this is causing problems as the last name will be displayed as august,
                inviteesGoing.push(`${keyName}, `);
            } else if (invitees[keyName] === false) {
                //this is causing problems as the last name will be displayed as august,
                inviteesNotGoing.push(`${keyName}, `);
            } else {
                console.error("undetermind user status:" + keyName);
            }
        });

thanks

3
  • Just want to point out, your JSON is showing margot.attending = “false” which is a strong but you are comparing it to false as a Boolean in your if statement. Commented May 12, 2020 at 17:31
  • Why are you putting commas in the array? If you want comma separators, add them when you use .join Commented May 12, 2020 at 17:35
  • If you use map() and the function doesn't return anything and you don't use the result, you should be using forEach(). Commented May 12, 2020 at 17:37

3 Answers 3

1

I've extended your example object a little bit and using a simple forEach solve your problem. You can try this:

const data = {
	 "invitees": {"gus": {"attending": true, "invitedBy": "will"}, "margot": {"attending": false, "invitedBy": "gus"}, "John": {"attending": true, "invitedBy": "gus"}, "doe": {"attending": true, "invitedBy": "John"}, "Alex": {"attending": false, "invitedBy": "John"}}
 };

const going = [], notGoing = [];

Object.entries(data.invitees).forEach(([name, value]) => {
    if (value.attending) {
	going.push(name);
    } else {
	notGoing.push(name);
    }
});

console.log('going', going);
console.log('not going', notGoing);
.as-console-wrapper{min-height: 100%!important; top: 0}

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

Comments

1

You can use reduce to keep both array. And while pushing data, add more details like attending, invitedBy. Use Object.entries, better control than Object.keys

const findInfo = (invitees) => {
  return Object.entries(invitees).reduce(
    ([going, notgoing], [name, { attending, invitedBy }]) => {
      let map = attending === true ? going : notgoing;
      map.push({
        name,
        attending,
        invitedBy,
      });
      return [going, notgoing];
    },
    [[], []]
  );
};
const findInfoWithout = (invitees) => {
  const [going, notgoing] = [[], []];
  Object.entries(invitees).forEach(([name, { attending, invitedBy }]) => {
    let map = attending === true ? going : notgoing;
    map.push({
      name,
      attending,
      invitedBy,
    });
  });
  return [going, notgoing];
};
const data = {
  HmS7XXPFoCQ7GmvfcCnF: {
    invitees: {
      gus: {
        attending: true,
        invitedBy: "will",
      },
      margot: {
        attending: "false",
        invitedBy: "gus",
      },
    },
  },
};

console.log(findInfo(data.HmS7XXPFoCQ7GmvfcCnF.invitees));
console.log(findInfoWithout(data.HmS7XXPFoCQ7GmvfcCnF.invitees));

Comments

1

This is how you can do it using mapValues in lodash :

const going = [];
const notGoing = [];

const guests = {
 "invitees": {
   "major": {
      "attending": true,
      "invitedBy": "will"
    },
   "tom": {
      "attending": true,
      "invitedBy": "gus"
     },
   "Ben": {
      "attending": false,
      "invitedBy": "gus"
    }
   }
};

_.mapValues(guests.invitees, (obj, guestName) => {
  obj.attending ? going.push(guestName) : notGoing.push(guestName)
})
console.log(going)
console.log(notGoing)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.