How to modify a Javascript object with the same keys as another array with numbers?

Asked

Viewed 164 times

0

I have an object with 20 keys in total, each key is associated to a value but, value that does not matter for the moment for the answer. So is the object:

var obj = {
   1: ...,
   2: ...,
   3: ...,
   ...,
   20: ...

}

I also have a array with numbers in a row, like this:

var myArray = [ 10, 11, 12, 13, 14, 15, 16, 17 ]

I want to return the same object (obj) but with the Keys same as mine array, example:

var obj = {
   10: ...,
   11: ...,
   12: ...,
   13: ...,
   14: ...,
   15: ...,
   16: ...,
   17: ...

}
  • Frederico, in [en.so] you should publish only in portuguese. Use the [Edit] button to translate.

  • You’re right Anderson, it’s fixed!

  • Missing the title xD

  • Now it’s time for once xD You can help me??

  • Not yet. Your object doesn’t seem to be a valid object in JS. You quote that it has 20 keys, but it looks more like a array. An object relates a key to a value and its objects in the question have no values.

  • Yes in fact each key is associated to a value, but at the moment I only want to work with the keys and hide those that are not in the array. I rewrote it so it’s easier to read.

Show 1 more comment

2 answers

1

First you must fetch the keys of your object, filter based on the array own and build a new object with the remaining keys. Search for the object keys with Object.keys, Filter with Array.prototype.filter and create a new object with Array.prototype.reduce.

const original = {
  1: 'a',
  2: 'b',
  3: 'c',
  4: 'd',
  5: 'e'
}

const manter = ['1', '3', '5']

// Busca as chaves do objeto
const chaves = Object.keys(original)

// Filtra com base no array
const mantidas = chaves.filter(chave => manter.includes(chave))

// Monta um novo objeto
const resultado = mantidas.reduce(
  (novo, chave) => {
    novo[chave] = original[chave]
    return novo
  },
  {}
)

console.log(resultado)

Remember that in Javascript the keys of an object at all times evening strings, same you create it with whole type keys. JS engine internally makes the cast for string when you don’t use this type, so the array of keys which must be kept shall contain strings or you can adapt the solution to make this conversion, given that objeto[1] will be the same as objeto["1"], but in the array [1] is not the same as ["1"].

0

I think I understand what you want to do, but I believe you can’t declare a dynamic object the way you described it. So try the following:

let myArray = [10,11,12,13,14,15,16,17]; //array c/ as chaves válidas
let obj = [{id:1,value:...},{id:2,value:...},{id:10,value:...}]; //remodelei seu objeto
//id é o valor correspondente às chaves do myArray
let newObj = []; //array do retorno
for(let key of obj) //foreach, cada chave do obj
{
   for(let id of myArray) //comparar c/ os n°s do myArray
   {
      if(key.id == id) //se o id for igual
      { 
         newObj.push(key); //adiciona ao obj de retorno
         break; 
      }
   }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.