[英]Search in an array of objects and return a value from the last corresponding Object in JS
I'm struggling to return a value for last event containing "SearchResults" in the case below: schema of my datalayer where I want to collect the information在以下情况下,我正在努力为包含“SearchResults”的最后一个事件返回一个值:我想要收集信息的数据层的架构
At the moment I achieved to write to following code in order to know if a SearchResult event exists or not:目前,我实现了编写以下代码以了解 SearchResult 事件是否存在:
//Check if an existing event contains Search Results
if (digitalData.event.filter(e => e.componentID === 'SearchResults').length > 0) {
// If there are any that exist, I want to take the last one that was generated and return the pageIndex value of that one
console.log("exist");
} else {
// If it doesn't exist return 1
return 1;
};
Now I'm struggling to find a way to select only the last event generated and return the value contained in "digitalData.event.attributes.pageIndex".现在我正在努力寻找一种方法来 select 仅生成最后一个事件并返回“digitalData.event.attributes.pageIndex”中包含的值。
Does anyone have any solution regarding this point?有人对此有任何解决方案吗?
Thank you,谢谢,
You could just save that in a variable temporarily and return the last result.您可以将其暂时保存在变量中并返回最后一个结果。 Is this what you wanted?这是你想要的吗?
const searchResults = digitalData.event.filter(e => e.componentID === 'SearchResults')
if (searchResults.length > 0) {
const yourAnswer = searchResults[searchResults.length -1]
console.log("exist");
} else {
return 1;
};
Filter the items first, and if there are any take the last item:首先过滤项目,如果有则取最后一个项目:
const searchResults = digitalData.event.filter(e => e.componentID === 'SearchResults');
if (searchResults.length > 0) {
// If there are any that exist, I want to take the last one that was generated and return the pageIndex value of that one
return searchResults[searchResults.length-1].pageLength;
} else {
// If it doesn't exist return 1
return 1;
};
Hello bro you can do what you want in this way.你好兄弟你可以用这种方式做你想做的事。
let getResult = digitalData.event.filter(e => e.componentID === 'SearchResults' && e.componentID.length > 0 )
if (getResult.length > 0) {
const getIt = getResult[getResult.length - 1];
getResult = getIt
}
console.log(getResult)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.