JavaScript How do i filter JSON based on a key, so i only have a list of one type of key for all objects filter json based on key value,filter json based on

[
  {
    "id":100,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  ...
]
Answer:1

For

Use:

function testIt()
{
  var sample = [
    {
      "id":100,
      "account_id":8,
      "name":"Out of Service",
      "default":false,
      "created_at":"2012-02-06T08:51:29.720-06:00",
      "updated_at":"2012-02-06T08:51:29.720-06:00"
    },
    {
      "id":101,
      "account_id":8,
      "name":"Out of Service",
      "default":false,
      "created_at":"2012-02-06T08:51:29.720-06:00",
      "updated_at":"2012-02-06T08:51:29.720-06:00"
    },
  ];  
    var result = sample.map(function(elt) { return elt.id; });
    Logger.log(result); //  [100.0, 101.0]

    }
Answer:2

Instead of Array.prototype.filter() you should use Array.prototype.map()

Code:

const data = [
  {
    "id":100,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  {
    "id":101,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
]

const result = data.map(obj => obj.id)

console.log(result)
Answer:3

You could alternatively use Array.reduce although I'm not sure there are any benefits over Yosvel's map method:

const data = [
  {
    "id":100,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  {
    "id":102,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
  {
    "id":105,
    "account_id":8,
    "name":"Out of Service",
    "default":false,
    "created_at":"2012-02-06T08:51:29.720-06:00",
    "updated_at":"2012-02-06T08:51:29.720-06:00"
  },
]

const res = data.reduce(
  (acc, val) => {
    return [...acc, val.id]
  },
  []
)
console.log(res)
Answer:4

I'm building a new website with a navbar with a dropdown menu and want to have the dropdown disappear when I click on another button or other part of the site. How do I code that? The code below is ...

I'm building a new website with a navbar with a dropdown menu and want to have the dropdown disappear when I click on another button or other part of the site. How do I code that? The code below is ...

I've made a modal with the help of javascript. How come the modal doesn't come out? These are my codes: <button class="jobview-control btn-primary jobview-btn" id="btn-show-modal">Recommend&...

I've made a modal with the help of javascript. How come the modal doesn't come out? These are my codes: <button class="jobview-control btn-primary jobview-btn" id="btn-show-modal">Recommend&...

  1. modal dialog does not show
  2. modal-dialog doesn't work
  3. modal popup doesn't show
  4. modal popup doesn't close
  5. modal popup doesn't show up
  6. modal-dialog-centered doesn't work
  7. bootstrap modal dialog doesn't close
  8. jquery modal dialog doesn't work
  9. jquery dialog modal doesn't work
  10. bootstrap modal dialog does not show
  11. jquery modal dialog does not close
  12. bootstrap modal dialog does not close

arr = Array(10).fill(false) arr[-2] = true console.log(arr) [false, false, false, false, false, false, false, false, false, false, -2: true] console.log(arr.length) // 10 I'm so surprised that ...

arr = Array(10).fill(false) arr[-2] = true console.log(arr) [false, false, false, false, false, false, false, false, false, false, -2: true] console.log(arr.length) // 10 I'm so surprised that ...

I have an ext:button. I want to trigger it's OnClick event by JavaScript based on the condition of a confirm message. My Button <ext:Button ID="btnAddEsya" Text="text" runat="server" Icon="Add" &...

I have an ext:button. I want to trigger it's OnClick event by JavaScript based on the condition of a confirm message. My Button <ext:Button ID="btnAddEsya" Text="text" runat="server" Icon="Add" &...

  1. triggering button click event in javascript
  2. triggering button click javascript
  3. triggering button click jquery