skip to Main Content

I’d like to get the numeric value of the object ‘add_ele’ from the following result using NodeJS, can somebody please show me how?

[
  { code: 'switch', value: true },
  { code: 'countdown_1', value: 0 },
  { code: 'add_ele', value: 151800 },
  { code: 'cur_current', value: 0 },
  { code: 'cur_power', value: 0 },
  { code: 'cur_voltage', value: 2140 },
  { code: 'test_bit', value: 0 },
  { code: 'voltage_coe', value: 0 },
  { code: 'electric_coe', value: 0 },
  { code: 'power_coe', value: 0 },
  { code: 'electricity_coe', value: 0 },
  { code: 'fault', value: 0 },
  { code: 'relay_status', value: 'last' },
  { code: 'child_lock', value: false }
]

I have not been able to fins a way to do this so far

2

Answers


  1. const arr = [
      { code: 'switch', value: true },
      { code: 'countdown_1', value: 0 },
      { code: 'add_ele', value: 151800 },
      { code: 'cur_current', value: 0 },
      { code: 'cur_power', value: 0 },
      { code: 'cur_voltage', value: 2140 },
      { code: 'test_bit', value: 0 },
      { code: 'voltage_coe', value: 0 },
      { code: 'electric_coe', value: 0 },
      { code: 'power_coe', value: 0 },
      { code: 'electricity_coe', value: 0 },
      { code: 'fault', value: 0 },
      { code: 'relay_status', value: 'last' },
      { code: 'child_lock', value: false }
    ]
    
    const result = arr.filter(item => item.code === 'add_ele')[0].value
    console.log(result)
    Login or Signup to reply.
  2. welcome to stackoverflow.
    You can try this code.

    Method 1:

    const arr = [
      { code: 'switch', value: true },
      { code: 'countdown_1', value: 0 },
      { code: 'add_ele', value: 151800 },
      { code: 'cur_current', value: 0 },
      { code: 'cur_power', value: 0 },
      { code: 'cur_voltage', value: 2140 },
      { code: 'test_bit', value: 0 },
      { code: 'voltage_coe', value: 0 },
      { code: 'electric_coe', value: 0 },
      { code: 'power_coe', value: 0 },
      { code: 'electricity_coe', value: 0 },
      { code: 'fault', value: 0 },
      { code: 'relay_status', value: 'last' },
      { code: 'child_lock', value: false }
    ]
    
    let result
    
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].code === 'add_ele') {
        result = arr[i].value;
        break;
      }
    }
    
    console.log(result);
    

    Method 2:

    const arr = [
      { code: 'switch', value: true },
      { code: 'countdown_1', value: 0 },
      { code: 'add_ele', value: 151800 },
      { code: 'cur_current', value: 0 },
      { code: 'cur_power', value: 0 },
      { code: 'cur_voltage', value: 2140 },
      { code: 'test_bit', value: 0 },
      { code: 'voltage_coe', value: 0 },
      { code: 'electric_coe', value: 0 },
      { code: 'power_coe', value: 0 },
      { code: 'electricity_coe', value: 0 },
      { code: 'fault', value: 0 },
      { code: 'relay_status', value: 'last' },
      { code: 'child_lock', value: false }
    ];
    
    const resObj = arr.find(item => item.code === 'add_ele');
    const result = resObj.value;
    
    console.log(result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search