Ian
Ian

Reputation: 354

How to narrow an array for a specific condition?

I have an array:

results = [
  {"Created Date ": "20181012", "Created By ": "A", "Job Number ": "001", "Department": "FS"},
  {"Created Date ": "20181012", "Created By ": "B", "Job Number ": "002", "Department": "DS"},
  {"Created Date ": "20181012", "Created By ": "C", "Job Number ": "004", "Department": "FS"}
]

Each value will like below sometime.(without blank char and column order changed, but the column name is fixed)

results = [
  {"Created By": "A", "Department": "FS", "Created Date": "20181012", "Job Number": "001" },
  {"Created By": "B", "Department": "DS", "Created Date": "20181012", "Job Number": "002"},
  {"Created By": "C", "Department": "FS", "Created Date": "20181012", "Job Number": "004"}
]

Now, I only want to get a new array that contains the value of 3 columns, "Created By", "Created Date", and "Job Number", with a specific order. The expected value should like:

results = [
  {"20181012", "A", "001"},
  {"20181012", "B", "002"},
  {"20181012", "C", "004"}
]

Is there any good solution?

Upvotes: 1

Views: 403

Answers (2)

slevy1
slevy1

Reputation: 3832

This demonstrates another way one might use an Array object's map() method, as follows:

var results = [{
"Created Date ": "20181012",
"Created By ": "A",
"Job Number ": "001",
"Department": "FS"
  },
  {
"Created Date": "20181012",
"Created By": "B",
"Job Number": "002",
"Department": "DS"
  },
  {
" Created Date": "20181012",
" Created By": "C",
" Job Number": "004",
"Department": "FS"
  }
];

Array.prototype.remove = function(index){
  this.splice(index,1);
}

var nu_arry = results.map(item => {
  let d = [];
  let keys = Object.keys(item);
  keys.remove(3);  // remove Department key name
  keys.forEach(function(key) {
    d.push(item[key]);
  });
  return d;
});
console.log(nu_arry);

Whether a space occurs or not prior to or at the end of a key name makes scant difference because the code is able to detect precisely the names of all the keys by utilizing Object.keys(). The advantage of this solution is that if ever a coder decided to modify the names of the keys, this code would still work, since there is no hard-coding of the variant keys. Note: the short arrow function syntax obviates the need to create a separate user-defined function.

Upvotes: 0

slider
slider

Reputation: 12990

First note that objects don't have reliable ordering. So first or second column doesn't make any sense.

But since you know the keys for which you want the value, you can use map and at the same time account for the extra char by considering both variations of the key:

var results = [{
    "Created Date ": "20181012",
    "Created By ": "A",
    "Job Number ": "001",
    "Department": "FS"
  },
  {
    "Created Date": "20181012",
    "Created By ": "B",
    "Job Number ": "002",
    "Department": "DS"
  },
  {
    "Created Date": "20181012",
    "Created By ": "C",
    "Job Number ": "004",
    "Department": "FS"
  }
]

var output = results.map(d => [
  d['Created Date'] || d['Created Date '],
  d['Created By'] || d['Created By '],
  d['Job Number'] || d['Job Number ']
]);

console.log(output);

Upvotes: 3

Related Questions