0

I want to access names from this data whose status is completed how do it do that? I have fetched this data from an API and I want to save the names into a state whose status is completed.

"data": [
  {
    "status": "COMPLETED",
    "_id": "606bf5d19173931121c0017c",
    "userId": {
      "_id": "606bf5d19173931121c0017a",
      "email": "[email protected]",
      "contactNumber": 7896541236
    },
    "name": "Admin1"
  },
  {
    "status": "PENDING",
    "_id": "606c074e9173931121c0017f",
    "userId": {
      "_id": "606c074e9173931121c0017d",
      "email": "[email protected]",
      "contactNumber": 9638527416
    },
    "name": "Admin3"
  },
  {
    "status": "PENDING",
    "_id": "606c07a99173931121c00182",
    "userId": {
      "_id": "606c07a99173931121c00180",
      "email": "[email protected]",
      "contactNumber": 9638527477
    },
    "name": "Admin4"
  },
]
3
  • What have you tried so far? Looking at this it seems it would be a straight forward reduce no? Commented Apr 11, 2021 at 22:08
  • I don't how to approach this so yeah i haven't tried i guess. Commented Apr 11, 2021 at 22:10
  • Have a read: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 11, 2021 at 22:10

4 Answers 4

1

data is an array. Arrays have different methods (functions) you can run. These allow you to find items or update the array. In this case filter, find or findIndex seem like a good fit. To learn more about Arrays see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

filter()

filter() will return an array of items that match your condition, in this case:

data.filter(item => item.status === "COMPLETED")

find()

Alternative, find will return the first item that matches your condition:

data.find(item => item.status === "COMPLETED")

findIndex()

findIndex is similar to find, but it returns the index instead of the actual item:

data.findIndex(item => item.status === "COMPLETED")

Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like this to filter data with completed status. It will filter all data with status equals to completed and then map their name to completedNames.

const completedNames=data.filter(people => people['status']==='COMPLETED').map(person => person['name'])

Also if you are using functional component you can use useState to assign this to a state:

import {useState} from 'React'

const [compNames, setCompNames] = useState([]);

.
.
const completedNames=data.filter(people => people['status']==='COMPLETED').map(person => person['name'])

setCompNames(completedNames);

Comments

1

Method filter will be a good option when filtering a data stack, for cases like this. But here are a few options you can go with;

var data = []

data = json.data.filter(d => d.status == 'COMPLETED')
console.log(data)

data = []
json.data.reduce(d => d.status == 'COMPLETED' && data.push(d))
console.log(data)

data = []
json.data.forEach(d => d.status == 'COMPLETED' && data.push(d))
console.log(data)

Comments

1

As a few other answers have pointed out, filter() provides an effective way of only retrieving those items from the data returned by your API that match the completed status, and then return the names from those items using map().

Here it is in action. Click "Run code snippet" below to see the result(s):

const data = [{ status: "COMPLETED", _id: "606bf5d19173931121c0017c", userId: { _id: "606bf5d19173931121c0017a", email: "[email protected]", contactNumber: 7896541236 }, name: "Admin1" }, { status: "PENDING", _id: "606c074e9173931121c0017f", userId: { _id: "606c074e9173931121c0017d", email: "[email protected]", contactNumber: 9638527416 }, name: "Admin3" }, { status: "PENDING", _id: "606c07a99173931121c00182", userId: { _id: "606c07a99173931121c00180", email: "[email protected]", contactNumber: 9638527477 }, name: "Admin4" }, ];

// if you want a filtered list of ALL details
let filteredData = data.filter(e => e.status === "COMPLETED");
console.log('filteredData =', filteredData);

// if you want a filtered list of ONLY names
let filteredNames = data.filter(e => e.status === "COMPLETED").map(e => e.name);
console.log('filteredNames =', filteredNames);
.as-console-wrapper { top: 0 !important; max-height: 100% !important } .as-console-row::after { display: none !important } .as-console-row code { display: block; border-width: 1px 0 0 0 !important }

.as-console { counter-reset: section; }
.as-console-row {
  display: block !important;
  width: 100% !important;
  position: relative;
}
.as-console-row::before {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  width: 24px;
  height: 24px;
  top: 50%;
  right: 15px;
  transform: translateY(-50%);
  counter-increment: section;
  content: counter(section);
  background-color: #333;
  border-radius: 12px;
  font-weight: bold;
  color: #fff;
  z-index; 900;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.