0

I am trying to make a parse an array by returning all the values that's in the array except for 1 property. I thought i could make use of [...] spreading but this includes all. Now that I wrote out the whole object, I worry about the future where extra properties are added to the array. is there any function in javascript that would allow me to do this? Any hints will be much appreciated!!!

// the original object

0: {
       id: 1
    isActive: true
    isClassification: false
    isEditable: true
    isRequired: false
    isTeamType: false
    name: "Business Unit"
    termGroup: {id: 1, name: "Team Classifications", isTenantWide: false, termSets: Array(0)}
    termGroupId: 1
    termGroupName: "Team Classifications"
    terms: (3) [{…}, {…}, {…}]
}

// my parsing code

// args:
// @return      {array}     -  returns an array containing the termsets only.

getTermSets() {
console.log(this._termSetsWithChilden);

return this._termSetsWithChilden.map(termSet => {
    return {
        id: termSet.id,
        isActive: termSet.isActive,
        isClassification: termSet.isClassification,
        isEditable: termSet.isEditable,
        isRequired: termSet.isRequired,
        isTeamType: termSet.isTeamType,
        name: termSet.name,
        termGroupId: termSet.termGroupId,
        termGroupName: termSet.termGroupName,
        numberOfTerms: termSet.terms.length
    };
});

}

// my retun value

0: {
    id: 1
    isActive: true
    isClassification: false
    isEditable: true
    isRequired: false
    isTeamType: false
    name: "Business Unit"
    numberOfTerms: 3
    termGroupId: 1
    termGroupName: "Team Classifications"
}
2
  • You could copy all the properties and then delete the one you don't want. Commented Sep 24, 2019 at 13:01
  • What property do you want to leave out?? Commented Sep 24, 2019 at 13:02

5 Answers 5

4

If I'm understanding what you want properly, you can use destructuring and rest notation for that:

return this._termSetsWithChilden.map(({propertyYouDontWant, ...rest}) => rest);

...where propertyYouDontWant is the property you want to leave out. (If there are multiple, just list them, e.g. ({thisOne, thatOne, theOtherOne, ...rest}).)

Live Example:

const array = [
  {one: "uno", two: "dos", three: "tres"},
  {one: "uno", two: "due", three: "tre"},
  {one: "un", two: "deux", three: "trois"}
];
const result = array.map(({three, ...rest}) => rest);
console.log(result); // Only has `one` and `two`

If you also want to add a property, you can do that with spread notation (but it creates an extra unnecessary object):

return this._termSetsWithChilden.map(({terms, ...rest}) => ({...rest, numberOfTerms: terms.length}));

...or just add it to the object created via rest:

return this._termSetsWithChilden.map(({terms, ...rest}) => {
    rest.numberOfTerms = terms.length;
    return rest;
});

Live Example:

const array = [
  {one: "uno", two: "dos", three: "tres"},
  {one: "uno", two: "due", three: "tre"},
  {one: "un", two: "deux", three: "trois"}
];
const result = array.map(({three, ...rest}) => {
  rest.threeLength = three.length;
  return rest;
});
console.log(result);

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

2 Comments

this is exactly what i was looking for, thanks a lot!
@RodneyWormsbecher - Someone said you also want to add a property. I've updated to show how to do that, too, if you do.
1

You can delete the specific property from the object.

  return this._termSetsWithChilden.map(termSet => {
        termSet.numberOfTerms= termSet.terms.length
        delete termSet.terms
        return termSet;
    });

2 Comments

Of course, this does modify the original, making map unnecessary.
He also wants to add a new property.
0

I think the best way would be to return the same object and change only what you want.

return this._termSetsWithChilden.map(termSet => {
    var t = {...termSet}; // create a copy
    t.numberOfTerms = t.terms.length;
    delete t.terms; // delete what we dont need 
    return t;
});

I hope this helps.

Comments

0

Yes you can use the ...-syntax:

let original = {
  id: 1,
  isActive: true,
  isClassification: false,
  isEditable: true,
  isRequired: false,
  isTeamType: false,
  name: "Business Unit",
  termGroup: {
    id: 1,
    name: "Team Classifications",
    isTenantWide: false,
    termSets: Array(0)
  },
  termGroupId: 1,
  termGroupName: "Team Classifications",
  terms: []
};

let {
  name,
  id,
  termGroup,
  ...filtered
} = original;

console.log(filtered);

Comments

0

In your map function, you can create a new object and copy the properties like accessing array, for example:

return this._termSetsWithChilden.map(termSet => {
    let ret = [];
    for (let key in termSet) {
        if (key == 'terms') {
            // ignore
            continue;
        } else if (key == 'terms') {
            ret['numberOfTerms'] = termSet[key].length;
        } else {
            ret[key] = termSet[key];
        }
    }
    return ret;
});

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.