0

I try to filter values from the next:

const data = [
    {
        car: 'audi',
        age: 2,
        power: 200
    },
    {
        car: 'bmw',
        age: 32,
        power: 100
    },
    {
        car: 'mercedes',
        age: 21,
        power: 150
    },
];

const getRes = (data, keyTxt) => {

    return data.map(i => {
        return {
            [keyTxt]: (
                Object.entries(i).filter(j => !'car'.includes(j))
            )
        }
    })
};
console.log(getRes(data, 'age'));

I try to check if word car exists in Object.entries(), and if it exists to delete all arrays which include the word and to get something like this:

  {
    "age": [
     
      [
        "age",
        2
      ],
      [
        "power",
        200
      ]
    ]
  },
  {
    "age": [
     
      [
        "age",
        32
      ],
      [
        "power",
        100
      ]
    ]
  },
  {
    "age": [
     
      [
        "age",
        21
      ],
      [
        "power",
        150
      ]
    ]
  }

What is wrong with my code and why the filter does no work? How to make this code to be able to get what i described above?

1
  • 4
    j.includes('car'). although it should really be j[0]!=='car' Commented Jun 17, 2020 at 6:20

4 Answers 4

1

Just a slight correction in .includes

const data = [
    {
        car: 'audi',
        age: 2,
        power: 200
    },
    {
        car: 'bmw',
        age: 32,
        power: 100
    },
    {
        car: 'mercedes',
        age: 21,
        power: 150
    },
];

const getRes = (data, keyTxt) => {

    return data.map(i => {
        return {
            [keyTxt]: (
                Object.entries(i).filter(j => !j.includes('car'))
            )
        }
    })
};
console.log(getRes(data, 'age'));

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

Comments

0

Should be something like this:

  Object.entries(i).filter(j => !j.includes('car'))

Comments

0

You have to either destructure the entries or reference them by index.

const getRes = (data, keyTxt) => {
  return data.map((entry) => {
    return {
      [keyTxt]: Object.entries(entry).filter(([key]) => key != 'car')
    };
  });
};

Comments

0

You could use .map() and use delete to delete the property you dont need. Then return an Object with with your dynamic key name and Object.entries() as its value

const data = [
    {
        car: 'audi',
        age: 2,
        power: 200
    },
    {
        car: 'bmw',
        age: 32,
        power: 100
    },
    {
        car: 'mercedes',
        age: 21,
        power: 150
    },
];


let result = (data, txt) => {
   return data.map(el => {
      delete el.car;
      return {
         [txt]: Object.entries(el)
      };
   })
}


console.log(result(data, "age"));

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.