0

I'm using a jQuery.Each() function to iterate over an Object fetched through an API. (Woocommerce API). Meta_data is being published in the object with value and key parameters.

I would like to the the value of the part where the key is _cpo_cart_weight. I can get this by using a numeric value like meta_data[6].value, but i prefer to get it by the key name. How can I do this?

Second question. Later in this object, line_items are being given. Can I iterate through these by using a second $.each function?

Answer (given by editor)

console.log(meta_data.find(item => item.key==="_cpo_cart_weight").value)
<script>
const meta_data = [{
    "id": 2036,
    "key": "is_vat_exempt",
    "value": "no"
  },
  {
    "id": 2037,
    "key": "_iconic_wds_is_same_day",
    "value": ""
  },
  {
    "id": 2038,
    "key": "_iconic_wds_is_next_day",
    "value": ""
  },
  {
    "id": 2039,
    "key": "jckwds_date",
    "value": "20/12/2021"
  },
  {
    "id": 2040,
    "key": "jckwds_date_ymd",
    "value": "20211220"
  },
  {
    "id": 2041,
    "key": "jckwds_timestamp",
    "value": "1639990800"
  },
  {
    "id": 2042,
    "key": "_cpo_cart_weight",
    "value": "0.5"
  },
  {
    "id": 2043,
    "key": "_woofunnel_cid",
    "value": "1"
  },
  {
    "id": 2047,
    "key": "_new_order_email_sent",
    "value": "true"
  },
  {
    "id": 2048,
    "key": "_wfacp_report_needs_normalization",
    "value": "yes"
  }
];
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Wouter
  • 41
  • 6
  • [Finding an object in an array by key value](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects). I'm not sure what you mean by "direct sibling" in the title, the question doesn't seem to mention siblings at all? – DBS Dec 15 '21 at 09:49
  • I updated your code with the answer. It is trivial and you do not need to use jQuery – mplungjan Dec 15 '21 at 09:57

0 Answers0