简体   繁体   English

当在数组中找到某个键​​值对时,如何返回步骤号 [i]?

[英]How can I return the step number [i] when a certain key-value pair is found in an array?

Super NOOB to javascript, so I need some help trying to parse this JSON object and return a specific value. Super NOOB to javascript,所以我需要一些帮助来尝试解析这个 JSON 对象并返回一个特定的值。

So I have a JSON payload from an API Call that I've turned into an object (sorry, I still don't know how to line break this so it shows easily here on Stack Overflow):所以我有一个来自 API 调用的 JSON 负载,我已经把它变成了一个对象(对不起,我仍然不知道如何换行,所以它很容易在 Stack Overflow 上显示):

var myObj = JSON.parse("{'ClassID':'1ghjk2345','ProcessName':'Workflow','Steps':[{'Id':'1234ghjhg102','Name':'Start','ActivityInstanceID':'12435r3ffe2'},{'Id':'134gbvgg102','Name':'attendance','ActivityInstanceID':'7865fdghd'},{'Id':'1jhgy102','Name':'lesson','ActivityInstanceID':'12gs5ghdse2'},{'Id':'1ghkb102','Name':'quiz','ActivityInstanceID':'1sgdtgt'},{'Id':'12fkfh02','Name':'evaluation','ActivityInstanceID':'1243sdfgssdfg2'},{'Id':'1khfhf02','Name':'dismissal','ActivityInstanceID':'124sdfgdfgrrrfe2'}],'TimeTaken':54838,'_id':'PK21342ffh','status':'Complete'}");

I would like to return the Step number in the Array that contains this key-value pair:我想返回包含此键值对的数组中的步骤编号:

'Name':'evaluation'

I need this because the order of the steps changes for each payload - sometimes it's step 4, sometimes 2 depending on when the teacher initiates that step in the application.我需要这样做是因为每个有效负载的步骤顺序都会发生变化 - 有时是第 4 步,有时是第 2 步,具体取决于教师在应用程序中启动该步骤的时间。

I would then like to retrieve the 'ActivityInstanceID' of that step.然后我想检索该步骤的“ActivityInstanceID”。

You need to change your data do fit json requirements.您需要更改数据以符合 json 要求。 Use variables names in lower case.使用小写的变量名称。

 var myData = JSON.parse('{"ClassID":"1ghjk2345","ProcessName":"Workflow","Steps":[{"Id":"1234ghjhg102","Name":"Start","ActivityInstanceID":"12435r3ffe2"},{"Id":"134gbvgg102","Name":"attendance","ActivityInstanceID":"7865fdghd"},{"Id":"1jhgy102","Name":"lesson","ActivityInstanceID":"12gs5ghdse2"},{"Id":"1ghkb102","Name":"quiz","ActivityInstanceID":"1sgdtgt"},{"Id":"12fkfh02","Name":"evaluation","ActivityInstanceID":"1243sdfgssdfg2"},{"Id":"1khfhf02","Name":"dismissal","ActivityInstanceID":"124sdfgdfgrrrfe2"}],"TimeTaken":54838,"_id":"PK21342ffh","status":"Complete"}'); for (let i = 0; i < myData.Steps.length; i++) { if(myData.Steps[i].Name === "evaluation") console.log("the step is: ", i); }

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

相关问题 如何在 JS 中不显示 function 返回消息的情况下获取键值对 - How can I get key-value pair(s) without displaying function return message in JS 如何根据每个项目的特定键值对在对象数组中分离相似或相似的项目? - How can I separate similar or alike items in an array of objects based on a specific key-value pair for each item? 如何从 JavaScript 对象数组中获取键值对? - How do I get key-value pair from array of JavaScript objects? 如何从此键值对中获取给定的突出显示的文本信息 - How can I obtain the given highlighted text information from this key-value pair 如何将对象数组转换为键对值 - How can i convert array of objects into key pair value 如何创建类似键值对的新数组? - How can I create new array of similar key value pair? 我可以将键值对添加到 object 并让键等于变量吗? - Can I add a key-value pair to an object and have the key be equal to a variable? 我可以将 function 存储在键值对中,其中 function 是 JavaScript 的键吗? - Can I store function in key-value pair where the function is the key for JavaScript? 如何在 javascript 中获取对象数组(键值对)的值? - How to get a value of array of objects (pair key-value) in javascript? 如何在JavaScript过滤器函数中访问键值对中的数组 - How to access an array in key-value pair in a javascript filter function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM