Extracting specific values from an array within an object

175 views Asked by At

I'm setting up a test to ensure that a faceted Solr query 'contents' are correctly displayed within a page element, using javascript.

The Solr query result, which I've named "ryanlinkstransmissionpage", is;

{ Transmission: [ 'Manual', 12104, 'Automatic', 9858 ] }

What I would like to do is extract the 'Manual' and 'Automatic' only, so I can then test that these values are displayed on a page.

However, it is more the functionality involved in this that I cannot get my head around, as I will be using this method on other Solr query results.

To possibly complicate things, this Solr query result "ryanlinkstransmissionpage" is from a dynamic 'live' Solr, so the values may change each time it's run (so there may be more or less values within this array when it's tested on the following day for example).

I've tried a few javascript commands, but to no avail.

JSON.parse(ryanlinkstransmissionpage)
JSON.stringify(ryanlinkstransmissionpage)
Object.values(ryanlinkstransmissionpage)

Any help would be greatly appreciated. Thanks.

2

There are 2 answers

5
MrTibbles On BEST ANSWER

If possible, i highyl recommend changing the transmission field to be an object, rather than an array. That will give you far greater ability to read the data within.

Ignoring that, are you looking to extract the string values and the number values that follow them? ie. "Manual" and "12104"? Or are you simply trying to assert that the string values are present on the page?

Either way, here are two possible approaches.

const ryanlinkstransmissionpage = { Transmission: [ 'Manual', 12104, 'Automatic', 9858 ] };
// Pull out the string values
const strngVals = ryanlinkstransmissionpage.Transmission.filter(val => typeof val === 'string');
// Pull out the string values and the numbers that follow
const strngNumVals = ryanlinkstransmissionpage.Transmission.reduce((keyVals, val, idx, srcArr) => {
  if (typeof val === 'string') keyVals[val] = srcArr[idx + 1];
  return keyVals;
}, {});

The reduce approach is not stable or robust to changes in data provided from this Solr query result you refer to, nor is it tested. #shrug

2
Djtouchette On

Javascript has a built in method called Array.prototype.find(() =>). If you just want to check if this value exists to ensure its on the page, you can simply do:

const ryanlinkstransmissionpage = { Transmission: [ 'Manual', 12104, 'Automatic', 9858 ] };

const manual = ryanlinkstransmissionpage.Transmission.find((ele) => ele === 'Manual'); // returns 'Manual'

const automatic = ryanlinkstransmissionpage.Transmission.find((ele) => ele === 'Automatic'); // returns 'Automatic'
console.log(automatic);
console.log(manual);

// or

const findInArray = (arr, toFind) => {
  const result = arr.find((ele) => ele === toFind);
  return !!result;
}

console.log(findInArray(ryanlinkstransmissionpage.Transmission, 'Automatic')); // true
console.log(findInArray(ryanlinkstransmissionpage.Transmission, 'HelloWorld')); // false
console.log(findInArray(ryanlinkstransmissionpage.Transmission, 'Manual')); // true