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

172
Visualizações
if object has key from different array object key value, return only that key

I have 2 arrays.

One array contains some people objects, the other array contains objects with name key that holds the value needed from the people objects.

My solution so far but not getting any luck....

When mapping over people array how do I return only certain properties from person? Not the entire person object

const customContactValues = people.map((person) => { return valuesNeeded.filter((el) => (el.name in person) ? person[el.name] : ""  ) })

console.log(customContactValues)

Here is my people array

    const people = [
    {
        "foods": {
            "favoriteFood": "Ice cream"
        },
        "firstName": "John",
        "lastName": "Doe",
        "age": 30

    },
    {
        "foods": {
            "favoriteFood": "pizza"
        },
        "firstName": "Jane",
        "lastName": "Doe",
        "age": 39
    },
    {
        "foods": {
            "favoriteFood": "pbj"
        },
        "firstName": "Kevin",
        "lastName": "Baker",
        "age": 22

    },
    {
        "foods": {
            "favoriteFood": "pushpops"
        },
        "firstName": "ernie",
        "lastName": "duncan",
        "age": 29
    },
]

Here is the values array which contains the keys I need from people array

const valuesNeeded = [ 
  { name: 'firstName' },
  { name: 'lastName' },
  { name: 'favoriteFood' }
]

I am trying to get and output like this below

const desiredResult = [
{firstName: "John", lastName: "Doe", favoriteFood: "Ice cream"},
{firstName: "Jane", lastName: "Doe", favoriteFood: "Pizza"},
{firstName: "Kevin", lastName: "Baker", favoriteFood: "pbj"},
{firstName: "ernie", lastName: "duncan", favoriteFood: "pushpops"}
]

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

0

filter only filters elements from array based on some condition but in your case we don't want to filter elements we just want to create new array of objects from and existing array so map function is a good start.

Second problem is the object can contain nested object which may contain required key value pair so to retrieve them we can recursively look over the value which is object if we don't find the key value directly in the object.

And since we don't want all the key value for each object in array we can either create a new object or delete from existing object, keeping the original is good and safe option if required for further processing.

const people = [{
    "foods": {
      "favoriteFood": "Ice cream"
    },
    "firstName": "John",
    "lastName": "Doe",
    "age": 30

  },
  {
    "foods": {
      "favoriteFood": "pizza"
    },
    "firstName": "Jane",
    "lastName": "Doe",
    "age": 39
  },
  {
    "foods": {
      "favoriteFood": "pbj"
    },
    "firstName": "Kevin",
    "lastName": "Baker",
    "age": 22

  },
  {
    "foods": {
      "favoriteFood": "pushpops"
    },
    "firstName": "ernie",
    "lastName": "duncan",
    "age": 29
  },
];

// function to retrieve the key value recursively
function valueByKey(obj, key) {
  let value = obj[key];
  if (value) {
    return value;
  }
  Object.keys(obj).forEach(function(k) {
    if (typeof obj[k] == 'object') {
      value = valueByKey(obj[k], key);
    }
  });
  return value;
}

const valuesNeeded = [{
    name: 'firstName'
  },
  {
    name: 'lastName'
  },
  {
    name: 'favoriteFood'
  }
];

// creating new object by passing the current object and key
let output = people.map(function(item) {
  let result = {};
  valuesNeeded.forEach(function(obj) {
    result[obj.name] = valueByKey(item, obj.name);
  });
  return result;
});

console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can search for the desired key on the top level, and go to child objects if not found. Here is my approach :

function getValue(obj, key) {
    const keys = Object.keys(obj);
    // Check if key exist on this level
    if (keys.includes(key)) {
        return obj[key];
    } else {
        // else we try to find in child objects
        for (k of keys) {
            if (typeof obj[k] == 'object') {
                return getValue(obj[k], key);
            }
        }
    }
    //Return this if not found
    return '';
}

const people = [
    {
        foods: {
            favoriteFood: 'Ice cream',
        },
        firstName: 'John',
        lastName: 'Doe',
        age: 30,
    },
    {
        foods: {
            favoriteFood: 'pizza',
        },
        firstName: 'Jane',
        lastName: 'Doe',
        age: 39,
    },
    {
        foods: {
            favoriteFood: 'pbj',
        },
        firstName: 'Kevin',
        lastName: 'Baker',
        age: 22,
    },
    {
        foods: {
            favoriteFood: 'pushpops',
        },
        firstName: 'ernie',
        lastName: 'duncan',
        age: 29,
    },
];

const valuesNeeded = [
    { name: 'firstName' },
    { name: 'lastName' },
    { name: 'favoriteFood' },
];

let output = people.map((item) => {
    let result = {};
    valuesNeeded.forEach(function (obj) {
        result[obj.name] = getValue(item, obj.name);
    });
    return result;
});

console.log(output);

Edit 1 : You can further simplify valuesNeeded to keysNeeded and store only the keys in an array like this keysNeeded = ['firatName', 'lastName', 'favoriteFood']. Please change invocation accordingly.

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