Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
VueJS and Lodash: How to get an array of objects that only contain a certain key string?

I am trying to cherry pick some data in an Object that have a string _new appended to it.

Codesandbox: https://codesandbox.io/s/vibrant-bardeen-77so1u?file=/src/components/Lodash.vue:0-353

I have some data like so:

data.json:

[
  {
    "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"
  }
]

Using lodash, I thought I would do something like this:

<template>
  <div>{{ newProperties }}</div>
</template>
<script>
import { omitBy } from "lodash";
import data from "../assets/data.json";
export default {
  data() {
    return {
      localShortNames: data,
    };
  },
  computed: {
    newProperties() {
      return omitBy(this.localShortNames, (v, k) => k.endsWith("_new"));
    },
  },
};
</script>

The problem is that the entire original array of objects is still being returned. How can I just get an array of objects that contain the _new property? If there is a better lodash method to use for this case, let me know, too!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda