3

If I have an object, such as,

const obj = {
  a: 1,
  b: 2,
  c: 3,
}

I can map the keys and values as,

Object.entries(obj).map(([key, value]) => console.log(`${key}: ${value}`))

Is it possible in Javascript to omit a property, when mapping it?

Something like,

Object.entries({a: obj.a, ...obj}).map(([key, value]) => console.log(`${key}: ${value}`))

I can do the following:

Object.entries(obj).map(([key, value]) => key !== 'a' && console.log(`${key}: ${value}`))

But I feel like there can be a cleaner way to do this, and also this wouldn't work, because that mapped index will contain undefined. Just looking to clarify this.

Mike K
  • 7,621
  • 14
  • 60
  • 120
  • 2
    Why are you using `.map()` here in the first place? You're not returning anything. Seems like you are using it as a `.forEach()`. – Ivar Jul 22 '20 at 14:15
  • 2
    If you want to skip properties I'd just chain a `.filter()` and then a `.map()` – Anthony Jul 22 '20 at 14:16
  • I'm mapping an object to JSX elements, and I need to exclude a specific key from a form data object. – Mike K Jul 22 '20 at 14:16
  • You can't achieve that by using `.map()` alone if you're using the return value. `.map()` iterates over every element and maps it to a new value. The array that is returned will always be the same length as the source array. – Ivar Jul 22 '20 at 14:20
  • 1
    @MikeK If you're mapping to jsx elements, then please show us the actual code that does that. – Bergi Jul 22 '20 at 14:22
  • If you wrap your method into a function you could destructure your input object to remove the `a` property and obtain the remaining properties like so: `const mapObj = ({a, ...obj}) => Object.entries(obj).map(([key, value]) =>\`${key}: ${value}\`)` – Nick Parsons Jul 22 '20 at 14:23
  • 1
    Perhaps useful is `const filterMap = (f, m) => (xs) => xs .flatMap ((x) => f (x) ? [m (x)] : [])` – Scott Sauyet Jul 22 '20 at 14:40

3 Answers3

2

you can use filter

Object.entries(obj).filter(([key, _]) => key !== "a").map();
adz5A
  • 2,012
  • 9
  • 10
  • You can use reduce after the filter to get an object back from the entries array. `Object.entries(obj).filter(([key, _]) => key !== "a").reduce((res, [key, value]) => ({ ...res, [key]: value}), {});` – Filipe Pinheiro Jul 22 '20 at 14:18
  • @FilipePinheiro OP doesn't want that? And no, you shouldn't use `reduce` for that, just call `Object.fromEntries` :-) – Bergi Jul 22 '20 at 14:21
  • TIL `fromEntries`. Thanks :) – Filipe Pinheiro Jul 22 '20 at 14:46
1

Here is what you want:

const obj = {
    a: 1,
    b: 2,
    c: 3,
}


const res = Object.entries(obj).filter(([key, value]) => key !== 'a');

console.log(res);
8HoLoN
  • 1,122
  • 5
  • 14
0

If you want to hide/exclude a key when using a map you can set its enumerable property to false, using Object.defineProperty:

const obj = {
  a: 1,
  b: 2,
  c: 3,
}

// this will hide key 'b' from obj
Object.defineProperty(obj, 'b', {
  enumerable: false,  
});

Object.entries(obj).map(([key, value]) => console.log(`${key}: ${value}`))
soltex
  • 2,993
  • 1
  • 18
  • 29