1

I have an array of column indexes that looks like, for example, [2, 4, 5] and an object with string value keys ranging from "0" through "6".

I want to create a new array containing all elements from my original object except those with keys matching any value in my column index array. This is how I'm doing it currently but I'm not happy with it:

const convertObjectToArray = (columnIndexes, rowObject) => {
    return Object.keys(rowObject).map(key => {
        let rowValue = '';
        if (columnIndexes.indexOf(parseInt(key)) === -1) {
            rowValue = rowObject['' + key];
        }
        return rowValue;
    });
};

Perhaps there is a very simple, straightforward way of doing this and I'm just missing it?

10
  • Could you give a sample input and output? Commented Oct 11, 2017 at 21:28
  • Object.keys(obj).filter( k => columnIndices.indexOf(parseInt(k)) >= 0).map( k => obj[k]) Commented Oct 11, 2017 at 21:30
  • Note that your current approach doesn't technically filter out the other elements; it replaces them with empty strings (which may or may not be what you want). Commented Oct 11, 2017 at 21:32
  • @zinfandel: Yes, good catch and an important point. The array I return should have the same number of elements in it as the original object. Commented Oct 11, 2017 at 21:36
  • In that case, your solution is already quite concise. As others have noted, you can replace the single if with a ternary expression: return columnIndices.indexOf(parseInt(k)) >= 0 ? '' : obj[k]. Commented Oct 11, 2017 at 21:41

3 Answers 3

1
const convertObjectToArray = (columnIndexes, rowObject) =>
  Object.keys(rowObject)
   .filter( key => columnIndexes.includes(+key))
   .map(key => rowObject[key])

Or if you really want empty strings:

const convertObjectToArray = (columnIndexes, rowObject) =>
  Object.entries(rowObject)
   .map(([key, value]) =>
     columnIndexes.includes(+key)
     ?value
     :""
   );
Sign up to request clarification or add additional context in comments.

Comments

0

A straightforward way is to reorganize your code to build the result array directly while looping over the rowObject:

function convertObjectToArray(columnIndexes, rowObject) {
  let res = [];
  for(let key in rowObject)
    if(columnIndexes.indexOf(+key) < 0)
      res.push(rowObject[key]);
  return res;
}

Working demo code in the snippet below:

let excludeColumns = [2, 4, 5];
let rowObject= {
 '0': 'value 0',
 '1': 'value 1',
 '2': 'value 2',
 '3': 'value 3',
 '4': 'value 4',
 '5': 'value 5',
 '6': 'value 6'
};

console.log(
  convertObjectToArray(excludeColumns, rowObject)
);

function convertObjectToArray(columnIndexes, rowObject) {
  let res = [];
  for(let key in rowObject)
    if(columnIndexes.indexOf(+key) < 0)
      res.push(rowObject[key]);
  return res;
}

Comments

0

Here's a solution very close to what you were working with:

const convertObjectToArray = (columnIndexes, rowObject) => {
    return Object.keys(rowObject).map(key => {
        let rowValue = '';
        if (columnIndexes.indexOf(key) === -1) {
            rowValue = rowObject[key];
        } 
        return rowValue;        
    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.