How to pass object name value pair as a parameter to the method?

Viewed 798

I have an array of object that returns the following data:

result [
  {name: "Desc", value: "7777 - Florida Hurricane"}
  {name: "DSTR_NR", value: "7777"}
  {name: "Desc", value: "7172 - Virginia Severe Storm(s)"}
  {name: "DSTR_NR", value: "7172"}
  {name: "Desc", value: "7002 - Maryland Hurricane"}
  {name: "DSTR_NR", value: "7002"}
]

I need to pass in the disaster number as a parameter to my getProcessList() method to fetch the process list by the disaster number once the response is successful.

if (response.type == API_DISASTER_BY_ID_GET_SUCCESS) {
  this.getProcessList();
}

Based on the response output above, how can I properly pass the "DSTR_NR" (disaster number) to my method?

UPDATE

Whichever disaster I choose from the disaster dropdown, only that disaster value needs to pass to the getProcessList() method so that the corresponding list of process can be fetch for the process dropdown. enter image description here

<Dropdown
  placeholder={placeholder}
  search
  selection
  clearable
  options={options}
  value={this.state.value}
  style={style}
  onChange={(e, d) => {
    let result = d.options.filter(d => d.value === 'DSTR_NR');
    console.log(result)
    this.setState({ value: result });
    this.props.saveReduxData(datapath, result);
  }}
/>
2 Answers

You can filter the array of objects from the response using Array.prototype.filter. Then you can pass the values from those "DSTR_NR" objects to your method. If you want to only have the values from each value key in the filtered array of objects, just use Array.prototype.map.

let result = [
  {name: "Desc", value: "7777 - Florida Hurricane"},
  {name: "DSTR_NR", value: "7777"},
  {name: "Desc", value: "7172 - Virginia Severe Storm(s)"},
  {name: "DSTR_NR", value: "7172"},
  {name: "Desc", value: "7002 - Maryland Hurricane"},
  {name: "DSTR_NR", value: "7002"}
];

// array of "DSTR_NR" key-value pairs
let disasterVals = result.filter(i => i.name === "DSTR_NR");
console.log(disasterVals);

// array of `value`'s
let values = disasterVals.map(i => i.value);
console.log(values);

function someMethod(val) {
  return "Disaster Val: " + val;
}

// access value from array of objects
console.log(someMethod(disasterVals[0].value));

// or access value from array of `value`'s
console.log(someMethod(values[1]));
console.log(someMethod(values[2]));

the result is an array and there are multiple "DSTR_NR" so there will be multiple values that can be passed as the parameters

you can use filter.

example:


result = [
  {name: "Desc", value: "7777 - Florida Hurricane"}
  {name: "DSTR_NR", value: "7777"}
  {name: "Desc", value: "7172 - Virginia Severe Storm(s)"}
  {name: "DSTR_NR", value: "7172"}
  {name: "Desc", value: "7002 - Maryland Hurricane"}
  {name: "DSTR_NR", value: "7002"}
]

// the filtered result will be
filteredResult = result.filter(d => d.name === 'DSTR_NR');

// the filtered result will be 
/*
[ 
  {name: "DSTR_NR", value: "7777"},
  {name: "DSTR_NR", value: "7172"},
  {name: "DSTR_NR", value: "7002"},
]

*/

to get just the numbers you can use map on the filtered result

onlyNumbers =  filteredResult.map(d => d.value);

/*
 output:- 
    ["7777", "7172", "7002"]
*/

in one liner:

result.filter(d => d.name === 'DSTR_NR').map(d => d.value);

/*
    output:- 
    ["7777", "7172", "7002"]
*/

the output can be simply passed as an array as a single parameter or you can use a spread operator to send multiple parameters based on the output.

output = ["7777", "7172", "7002"];

// as a single paramater (array)
myFync(output);

// as multiple paramaters
myFunc(...output);

Related