最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter nested object - Stack Overflow

programmeradmin3浏览0评论

I receive an object that looks like this:

 this.tokensData = {
    O: {
        id: 0,
        name: value1,
        organization: organization1,
        ...,
       },
    1: {
        id: 1,
        name: value1,
        organization: organization1,
        ...,
        },
    2: {
        id: 2,
        name: value2,
        organization: organization2,
        ...,
        },
    ...
   }

I want to filter by id and remove the Object which id matches the id I receive from the store. What I tried so far:

const filteredObject = Object.keys(this.tokensData).map((token) => {
  if (this.$store.state.id !== this.tokensData[token].id) {
    return this.tokensData[token];
  }
});

This replaces the Object with undefined - which would work for my purposes but is obviously not ideal. Any help is much appreciated!

I receive an object that looks like this:

 this.tokensData = {
    O: {
        id: 0,
        name: value1,
        organization: organization1,
        ...,
       },
    1: {
        id: 1,
        name: value1,
        organization: organization1,
        ...,
        },
    2: {
        id: 2,
        name: value2,
        organization: organization2,
        ...,
        },
    ...
   }

I want to filter by id and remove the Object which id matches the id I receive from the store. What I tried so far:

const filteredObject = Object.keys(this.tokensData).map((token) => {
  if (this.$store.state.id !== this.tokensData[token].id) {
    return this.tokensData[token];
  }
});

This replaces the Object with undefined - which would work for my purposes but is obviously not ideal. Any help is much appreciated!

Share Improve this question edited Apr 15, 2020 at 13:48 twharmon 4,2825 gold badges27 silver badges50 bronze badges asked Apr 15, 2020 at 13:43 suuuriamsuuuriam 74714 silver badges27 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Try to use Object.entries and then Object.fromEntries() to create an object from a list of key-value pairs:

const store = [0 , 1];

const result = Object
   .entries(tokensData)
   .filter(([k, v]) => !store.some(s => s == v.id));

console.log(Object.fromEntries(result));

An example:

const tokensData = {
   O: {
       id: 0,
       name: '',
       organization: '',
      },
   1: {
       id: 1,
       name: '',
       organization: '',
       },
   2: {
       id: 2,
       name: '',
       organization: '',
       }
  }

const store = [0 , 1];

const result = Object
    .entries(tokensData)
    .filter(([k, v]) => !store.some(s => s == v.id));

console.log(Object.fromEntries(result));

You can do this by using Object.entries and Object.fromEntries:

const filteredObject = Object.fromEntries(
  Object.entries(this.tokensData).filter(
    ([key, value]) => value.id !== this.$store.state.id
  )
)

This can be done by cloning the object and removing the object at the ID:

const removeObjectByID = (obj, id) => {
  // first create a copy of the object
  const copy = JSON.parse(JSON.stringify(obj))
  // next, delete the one entry you don't want
  delete copy[id]
  // finally, return the new object
  return copy
}

// Test
const data = {a:1, b: 2, c: 3}

console.log(removeObjectByID(data, 'b')) // { a: 1, c: 3 }

The problem with undefined is caused by using this in your arrow function. Javascript scoping gives a different meaning to this if the function is a arrow function.

Furthermore I suggest to use filter.

.map is used to transform A -> B.

.filter should be used to filter out objects.

Now if we bine that this would bee something like this.

function filterById(token) {
  return this.$store.state.id !== this.tokensData[token].id;
}

function getTokenData(token) {
  return this.tokensData[token]
}

const token = Object.keys(this.tokensData)
  .filter(filterById)
  .map(getTokenData);
});

Please note that I'm not using arrow functions. Arrow function can't refer to this due to the way javascript handles scoping.

An alternative approach could be to reference this into a variable, so your arrow function can access the variable.

const self = this;
const token = Object.keys(this.tokensData)
  .filter(token => self.$store.state.id !== self.tokensData[token].id)
  .map(token => self.tokensData[token]);

Too make it even nicer you could utilize Object.entries. This will return an array of key and value, which you can destructure using es6 syntax as following [key, value].

const self = this;
const token = Object.entries(this.tokensData)
  .filter(([key, value]) => self.$store.state.id !== value.id)
  .map(([key, value]) => value);
发布评论

评论列表(0)

  1. 暂无评论