简体   繁体   English

如何通过 function 中的名称查找数组元素的索引?

[英]how to find index of array element by the name of it inside a function?

I have a js function that reads a JSON file and outputs them with a loop:我有一个 js function 读取 JSON 文件并通过循环输出它们:

response.data.forEach((item,index)=>{

  items += " <div class='eleman'>"
        + "<img src='"+item.teamMember.profileImageUrl+"' width='100%'>"
        + "<div class='content list'>"
        + "<div class='fullname'>"+item.teamMember.fullName+"</div>"+

    ...

There is an array on that json called customFields (item.teamMember.customFields) and it could have 0 to 7 indexes, example for one of them should be like: json 上有一个名为 customFields (item.teamMember.customFields) 的数组,它可能有 0 到 7 个索引,其中一个索引的示例应该是:

id    : 1202
name  : Tags
type  : SingleLineText
value : Composer,Music,Pan's Labyrinth

I want to get the value of the index that's name is ByLine.我想获取名为 ByLine 的索引的值。 And use it under the full name div.并在全名div下使用。

I tried several codes but they didn't work.我尝试了几个代码,但它们都不起作用。 Would appreciate if anyone can help.如果有人可以提供帮助,将不胜感激。

Below are assumed from the question:以下是从问题中假设的:

  1. response.data is an Array response.data是一个数组
  2. Each element in this Array is an object这个数组中的每个元素都是一个 object
  3. Each such object has a prop named teamMember每个这样的 object 都有一个名为teamMember的道具
  4. Each teamMember has a prop named customFields which is an Array每个teamMember都有一个名为customFields的道具,它是一个数组
  5. Each element of the Array (at point 4 above) has a prop name Array 的每个元素(在上面的第 4 点)都有一个道具name

The objective is to filter only those elments from customFields array where the element's name prop matches the value ByLine .目标是仅从customFields数组中过滤元素name prop 与值ByLine匹配的那些元素。

One possible way to achieve this:实现这一目标的一种可能方法:

const getIndexOfByLine = (arr = item.teamMember.customFields, matchNameWith = 'ByLine') => (
  arr
    .map((el, i) => ({elt: el, idx: i}))
    .filter(obj => obj.name === matchNameWith)
    .map(obj => obj.idx)
    [0]
);

NOTE: If more than 1 element has name as 'ByLine' then only the first index is returned.注意:如果超过 1 个元素的name为“ByLine”,则仅返回第一个索引。

Explanation解释

  • Iterate through the item.teamMember.customFields array using map with element as el and index as i .使用map遍历item.teamMember.customFields数组,其中元素为el ,索引为i
  • For each element in the array, return an object with two props namely elt and idx对于数组中的每个元素,返回一个 object 和两个 props,即eltidx

.map((el, i) => ({elt: el, idx: i}))

  • In the resulting array, apply the filter and keep only those elements where name matches ByLine (can be changed via parameter matchNameWith )在结果数组中,应用过滤器并仅保留nameByLine匹配的那些元素(可以通过参数matchNameWith更改)

.filter(obj => obj.name === matchNameWith)

  • Now, we have an array with objects that have the two props elt and idx .现在,我们有一个包含两个道具eltidx的对象的数组。 Since we only require the index, map the array to get only the index.由于我们只需要索引, map数组只获取索引。

.map(obj => obj.idx)

  • In case there were more than 1 elements that had 'ByLine' as the name , simply use the first occurance.如果有多个元素以 'ByLine' 作为name ,只需使用第一次出现。

[0]

I used this and it worked我用了这个,它奏效了

let byline=item.teamMember.customFields.filter(checkbyline);


function checkbyline(fields) {
  if (fields.name == 'Byline') {
  return fields.value}
else {return }
}   

and to use it on the HTML output并在 HTML output 上使用它

byline[0].value

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

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