[Vuejs]-VueJS and Lodash: How to get an array of objects that only contain a certain key string?

0👍

If I understood you correctly, take a look at following snippet (without lodash):

new Vue({
  el: '#demo',
  data() {
    return {
      localShortNames: [
        {"localSnameID": 18040, "isoGeoPoliticalEntry_FK": 959368, "isoLocalShortName_FK": 953, "shortNameID": 953, "adminLanguage2Char": "fa", "adminLanguage3Char": "fas", "name": "Afghānistān", "iso6393Char3Code": "fas", "P_precedingItem_FK": -1, "entryType": 0},
        {"localSnameID": 18312, "isoGeoPoliticalEntry_FK": 959368, "isoLocalShortName_FK": 977, "shortNameID": 977, "P_precedingItem_FK": 4, "entryType": 2, "name": "Afghānistān", "name_new": "AfghānistānXYZ", "iso6393Char3Code": "pus", "iso6393Char3Code_new": "eng", "adminLanguage2Char": "ps", "adminLanguage2Char_new": "en", "adminLanguage3Char": "pus", "adminLanguage3Char_new": "eng"}
      ]
    }
  },
  computed: {
    newProperties() {
      const res = []
      this.localShortNames.forEach(v => {
        const u = {}
        for(const prop in v) {
          if(prop.endsWith("_new")) u[prop]= v[prop]
        }
        if(Object.keys(u).length !== 0) res.push(u)
      })  
      return res
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div>{{ newProperties }}</div>
</div>

Leave a comment