简体   繁体   English

从对象内的数组中提取特定值

[英]Extracting specific values from an array within an object

I'm setting up a test to ensure that a faceted Solr query 'contents' are correctly displayed within a page element, using javascript. 我正在设置一个测试,以确保使用JavaScript在页面元素内正确显示多面Solr查询“内容”。

The Solr query result, which I've named "ryanlinkstransmissionpage", is; 我已将Solr查询结果命名为“ ryanlinkstransmissionpage”;

{ Transmission: [ 'Manual', 12104, 'Automatic', 9858 ] } {传送:['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. 但是,更多的功能无法解决,因为我将在其他Solr查询结果上使用此方法。

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). 为了使事情复杂化,此Solr查询结果“ ryanlinkstransmissionpage”来自动态的“实时” Solr,因此每次运行时值可能都会更改(因此,在第二天进行测试时,此数组中可能有更多或更少的值)例)。

I've tried a few javascript commands, but to no avail. 我已经尝试了一些javascript命令,但无济于事。

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

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

If possible, i highyl recommend changing the transmission field to be an object, rather than an array. 如果可能,我会建议将transmission字段更改为对象而不是数组。 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? 忽略这一点,您是否要提取string值和它们number值? ie. 即。 "Manual" and "12104"? “手册”和“ 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. reduce方法对于您引用的此Solr查询结果提供的数据更改不稳定或健壮 ,也未经过测试。 #shrug #耸肩

Javascript has a built in method called Array.prototype.find(() =>) . Javascript有一个名为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM