1

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.

Darren Harley
  • 75
  • 1
  • 18
  • What is `ryanlinkstransmissionpage` ? An object or a string? – Jonas Wilms Apr 10 '19 at 15:23
  • Which "specific values" ? All strings inside "Transmission" or which other pattern is there? – Jonas Wilms Apr 10 '19 at 15:24
  • I'm presuming that ryanlinkstransmissionpage is an **array** in an **object**, but I'm only basing my presumption on the fact that the [] brackets are contained in {} brackets. – Darren Harley Apr 10 '19 at 21:36
  • The specific values in this case that I require is the **"Manual"** and **"Automatic"** entries **only**. Although, as mentioned elsewhere, these specific values are dynamic, so the next time the solr query is run (which is where these values come from), either the "Manual" or "Automatic" values might not be present (and other values may be present). Thanks – Darren Harley Apr 10 '19 at 21:37

2 Answers2

1

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

MrTibbles
  • 66
  • 4
  • Thanks for you reply. I'm looking to extract, in this case, the **"Manual"** and **"Automatic" only**, and **not** the number values. I'm then looking at testing that the "Manual" and "Automatic" are displayed in an element on the page. – Darren Harley Apr 10 '19 at 21:33
  • Hmm ok, so I tried **const strngVals = ryanlinkstransmissionpage.Transmission.filter(val => typeof val === 'String');** and I got an empty array as the result (i.e. a [] ) Thanks – Darren Harley Apr 10 '19 at 22:02
  • Apologies, silly of me, it should be `typeof val === 'string'. Note that `string` is **lowercase**, not capitalised. – MrTibbles Apr 11 '19 at 07:30
  • Great, thanks. I've now tried this and it works just how I wanted it. Many, many thanks for your help :) – Darren Harley Apr 11 '19 at 10:02
  • Glad i could help, it is a team game after all +1 – MrTibbles Apr 11 '19 at 10:34
-1

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
Djtouchette
  • 395
  • 2
  • 6
  • 1
    If you just want to know if an array contains a specific value, you should use [`Array#includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes), not `Array#find`: `ryanlinkstransmissionpage.Transmission.includes('Manual')`. – Jordan Running Apr 10 '19 at 20:39
  • Thanks for you reply. The problem with this is that the "Manual" and "Automatic" are not 'fixed' values. As mentioned in my question, the next time my solr query is run (which is where these values come from), either "Manual" or "Automatic" may not be present, so I can't be sure that the array contains the specific values. – Darren Harley Apr 10 '19 at 21:31