简体   繁体   English

使用Javascript从数组或对象中提取value属性

[英]Extract a value property from either an array OR object using Javascript

I have some options in a dropdown box on my website which are then sent via webhook for me to extract details from. 我在网站的下拉框中有一些选项,然后通过webhook发送给我,以从中提取详细信息。 I am trying to extract the value from the ageId object. 我正在尝试从ageId对象中提取

But depending on the dropdown item selected, it is either placed into an array or an object with some other details added. 但是根据所选的下拉项,它可以放置在数组中,也可以是添加了其他一些详细信息的对象。

年龄下拉

When the Adult option is chosen, ageId is: [ { i: 0, value: '25', label: 'Adult' } ] 选择“ 成人”选项后,ageId为: [ { i: 0, value: '25', label: 'Adult' } ] '25 [ { i: 0, value: '25', label: 'Adult' } ]

When the Senior option is chosen, ageId is: { '1': { i: 1, value: '65', label: 'Senior' } } 选择“ 高级”选项时,ageId为: { '1': { i: 1, value: '65', label: 'Senior' } } '65 { '1': { i: 1, value: '65', label: 'Senior' } }

Before I added more dropdown options, the following worked to extract the required property of 25 在添加更多下拉选项之前,以下工作提取了25的必需属性

const ageMinimum = event.ageId[0].value[Object.keys(event.ageId[0].value)[0]].value;

How can I extract the value (eg.25 or 65) property every time regardless of the dropdown selected and the subsequent changing syntax? 我如何每次选择提取下拉列表和随后更改语法时都提取 (例如25或65)属性?

You could try: 您可以尝试:

var adult = [ { i: 0, value: '25', label: 'Adult' } ];
var senior = { '1': { i: 1, value: '65', label: 'Senior' } };
var tmp = senior; // Or adult

var ageMinimum  = (Array.isArray(tmp) ? tmp[0] : tmp[Object.keys(tmp)[0]]).value; // Edited to work with flexible first object property names
console.log(ageMinimum);

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

相关问题 使用可变属性值从JavaScript数组中删除对象 - Delete object from javascript array using variable property value JavaScript:从对象数组中的对象数组中提取属性值作为数组 - JavaScript: from an array of objects of an array of objects, extract value of a property as array 如何从 javascript 中的 object 数组中提取特定属性(即键/值)? - how to extract particular property(i.e key/value) from array of object in javascript? 数组中的Javascript对象属性值 - Javascript Object property value from an array 如何使用javascript从对象数组中提取对象的值 - How can I extract the value of an object from an array of objects array using javascript 从 javascript 中提取数组 object - Extract array from javascript object Javascript:使用第二个数组中的值提取数组中的密钥对值 - Javascript: Extract keypair value in array using values from second array 如何使用另一个数组按键/值从数组中提取对象 - How extract object by key/value from array using an other array 当输入是属性值 Javascript 数组时,从 Array 对象中删除对象具有相同的属性值 - Remove Object have same property value from Array object when input is Array of property value Javascript 从对象数组中提取属性值作为数组 - From an array of objects, extract value of a property as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM