0

I'm trying to create new object with different properties name from Array.

Array is:

profiles: Array(1)
   0:
    column:
        name: "profileName"
        title: "Profile name"
    status: "Active"

I want to create new function that return object with two properties:

id: 'profileName', profileStatus: 'Active'

The function that I have create is returning only one property as undefined undefined=undefined.

function getProfile(profiles) {
    if (!profiles.length) return undefined;

    return profiles.reduce((obj, profile) => {
        console.log('profiles', profile);
        return ({
          ...obj,
          id: profile.column.name,
          profileStatus: profile.status,
        });
      }, {});
}

The function getProfile is taking as input array 'profiles' from outside,

4
  • 2
    You wrote "profiles" instead of "profile" in the reduce method. Commented Jul 23, 2020 at 21:22
  • your item is called profile and you are accessing the array "profiles" Commented Jul 23, 2020 at 21:23
  • opps, my mistake where I did ask. Is not the issue here Commented Jul 23, 2020 at 21:24
  • Definitely would cause the undefined... Commented Jul 23, 2020 at 21:59

3 Answers 3

1

I've just tested here and this seems to be working actually

const getProfile1 = (p) => p.reduce((obj, profile) =>({
    ...obj,
    id: profile.column.name,
    profileStatus: profile.status,
}), {});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use map as an alternative.

var profiles = [{"column":{"name": "profileName3","title": "3Profile name"},"status": "Active"},{"column":{"name": "profileName","title": "Profile name"},"status": "Active"}];

function getProfile(profiles) {
    if (!profiles.length) return undefined;
    return profiles.map(function(profile,v){
      return {id:profile.column.name,profileStatus: profile.status};
   });

}

console.log(getProfile(profiles));

Comments

0

Whenever I use reduce in this way, I usually index the final object by some sort of an id. As noted in another answer, you could use map in this situation as well. If you really want your final data structure to be an object, however, you could do something like this:

/**
 * returns object indexed by profile id
 */
const formatProfiles = (profiles) => {
  return profiles.reduce((obj, profile) => {
    return {
      ...obj,
      [profile.id]: {
        id: profile.column.name,
        profileStatus: profile.status,
      }
    };
  }, {});
};

const profiles = [
  {
    id: 0,
    status: 'active',
    column: {
      name: "profile_name_1",
      title: "profile_title_1",
    },
  },
  {
    id: 1,
    status: 'inactive',
    column: {
      name: "profile_name_2",
      title: "profile_title_2",
    }
  }
];

const result = formatProfiles(profiles);

/**
 * Result would look like this:
 */
// {
//   '0': { id: 'profile_name_1', profileStatus: 'active' },
//   '1': { id: 'profile_name_2', profileStatus: 'inactive' }
// }

Comments

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.