0

What I am trying to do exceeds my knowledge. Thank you all for your time and help, it is a great pleasure to have the support of such a large community of great developers.

The problem

I need to loop over an object (JSON response) to determine which data is true and then edit the html with the results.

json object is:

  var data = {
  "total": 4,
  "limit": 50,
  "questions": [{
    "date_created": "2015-06-29T18:24:25.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "Pregunta de Testeo, user 2.",
    "id": 3612747353,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:30:16.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "Lorem ipsum dolor sit amet",
    "id": 3612938882,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:30:35.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "UNANSWERED",
    "text": "an est odio timeam quaerendum",
    "id": 3612752695,
    "deleted_from_listing": false,
    "hold": false,
    "answer": null,
    "from": {
      "id": 186625262,
      "answered_questions": 0
    }
  }, {
    "date_created": "2015-06-29T18:31:32.000-04:00",
    "item_id": "MLA567045929",
    "seller_id": 186626557,
    "status": "ANSWERED",
    "text": "Responder esta pregunta",
    "id": 3612753455,
    "deleted_from_listing": false,
    "hold": false,
    "answer": {
      "text": "Pregunta respondida",
      "status": "ACTIVE",
      "date_created": "2015-06-29T18:31:58.000-04:00"
    },
    "from": {
      "id": 186625262,
      "answered_questions": 1
    }
  }],
  "filters": {
    "limit": 50,
    "offset": 0,
    "is_admin": false,
    "sorts": [],
    "caller": 186626557,
    "seller": "186626557"
  },
  "available_filters": [{
    "id": "item",
    "name": "Item",
    "type": "text"
  }, {
    "id": "from",
    "name": "From user id",
    "type": "number"
  }, {
    "id": "totalDivisions",
    "name": "total divisions",
    "type": "number"
  }, {
    "id": "division",
    "name": "Division",
    "type": "number"
  }, {
    "id": "status",
    "name": "Status",
    "type": "text",
    "values": ["BANNED", "CLOSED_UNANSWERED", "DELETED", "DISABLED", "UNDER_REVIEW"]
  }],
  "available_sorts": ["item_id", "from_id", "date_created", "seller_id"]
};

The result I'm looking for is:

Of the data object, I need to extract questions with the status unanswered and the id field associated with these unanswered questions.

 "questions1":[{  "status" : "UNANSWERED",
                "id" : 3612747353}],
"questions2":[{  "status" : "UNANSWERED",
                "id" : 3612938882}],
 ...

Based on what I've searched, I've tried with loops, for in, and each without success.

Any suggestions or ideas on how I could achieve the desired result? I need to apply this example to several objects.

3 Answers 3

2

Try some handy list processing functions to simplify it conceptually. The filter and map functions will help. The function I provided to filter tells it to only let through items that meet the condition of having an unanswered status. The function I provided to map turned all the objects that came out of filter into just their ids.

data["questions"].filter(function(obj) {
    return obj["status"] === "UNANSWERED";
}).map(function(obj) {
    return obj["id"];
});
Sign up to request clarification or add additional context in comments.

Comments

1
var results = [];
for (var question in data.questions) {
  if (data.questions[question].status === "UNANSWERED") {
    results.push({
      "status" : data.questions[question].status,
      "id" : data.questions[question].id
    });
  }
}

// Now results contains an array of unanswered questions,
// with just their status & id.

1 Comment

some explanation of what you are doing will help the person asking the question
0

You can just loop through the questions and save the ids you want to an array:

var questions = data.questions;
var unanswered = [];
for(var i = 0, len = questions.length; i < len; i++) {
    if(questions[i].status === 'UNANSWERED') {
        unanswered.push(questions[i].id);
    }
}

unanswered will be an array of unanswered question ids. You don't need to save the status; you know they are all 'UNANSWERED'.

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.