filter and append AJAX JSON result based on objects with a specific key value
up vote
0
down vote
favorite
I'm trying to display the 3 records that have the value "sort":"list:"
I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!
$.ajax({
type: 'GET',
url: '/stores',
success: function(stores) {
// if (stores.sort === 'list') {
$.each(stores, function(i, store) {
// if (stores.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
// }
});
//}
}
});
My JSON is pretty simple:
[
{
"_id": "5be78df2fb6fc06239e0c39b",
"name": "Albertsons",
"sort": "list"
},
{
"_id": "5be78e00fb6fc06239e0c39c",
"name": "COSTCO",
"sort": "list"
},
{
"_id": "5be78e17fb6fc06239e0c3ac",
"name": "Food Lion",
"sort": "bank"
},
{
"_id": "5be78e34fb6fc06239e0c3b1",
"name": "7Eleven",
"sort": "list"
},
{
"_id": "5be78e5ffb6fc06239e0c3b7",
"name": "Kroger",
"sort": "bank"
}
]
jquery json ajax
add a comment |
up vote
0
down vote
favorite
I'm trying to display the 3 records that have the value "sort":"list:"
I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!
$.ajax({
type: 'GET',
url: '/stores',
success: function(stores) {
// if (stores.sort === 'list') {
$.each(stores, function(i, store) {
// if (stores.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
// }
});
//}
}
});
My JSON is pretty simple:
[
{
"_id": "5be78df2fb6fc06239e0c39b",
"name": "Albertsons",
"sort": "list"
},
{
"_id": "5be78e00fb6fc06239e0c39c",
"name": "COSTCO",
"sort": "list"
},
{
"_id": "5be78e17fb6fc06239e0c3ac",
"name": "Food Lion",
"sort": "bank"
},
{
"_id": "5be78e34fb6fc06239e0c3b1",
"name": "7Eleven",
"sort": "list"
},
{
"_id": "5be78e5ffb6fc06239e0c3b7",
"name": "Kroger",
"sort": "bank"
}
]
jquery json ajax
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to display the 3 records that have the value "sort":"list:"
I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!
$.ajax({
type: 'GET',
url: '/stores',
success: function(stores) {
// if (stores.sort === 'list') {
$.each(stores, function(i, store) {
// if (stores.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
// }
});
//}
}
});
My JSON is pretty simple:
[
{
"_id": "5be78df2fb6fc06239e0c39b",
"name": "Albertsons",
"sort": "list"
},
{
"_id": "5be78e00fb6fc06239e0c39c",
"name": "COSTCO",
"sort": "list"
},
{
"_id": "5be78e17fb6fc06239e0c3ac",
"name": "Food Lion",
"sort": "bank"
},
{
"_id": "5be78e34fb6fc06239e0c3b1",
"name": "7Eleven",
"sort": "list"
},
{
"_id": "5be78e5ffb6fc06239e0c3b7",
"name": "Kroger",
"sort": "bank"
}
]
jquery json ajax
I'm trying to display the 3 records that have the value "sort":"list:"
I've tried the two methods below of using an if statement to filter results, (commented out below) to display the 3 records but both methods did not render any results to the screen? With the lines commented out as below the page correctly renders all 5 results. Any advice is appreciated THANKS!
$.ajax({
type: 'GET',
url: '/stores',
success: function(stores) {
// if (stores.sort === 'list') {
$.each(stores, function(i, store) {
// if (stores.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
// }
});
//}
}
});
My JSON is pretty simple:
[
{
"_id": "5be78df2fb6fc06239e0c39b",
"name": "Albertsons",
"sort": "list"
},
{
"_id": "5be78e00fb6fc06239e0c39c",
"name": "COSTCO",
"sort": "list"
},
{
"_id": "5be78e17fb6fc06239e0c3ac",
"name": "Food Lion",
"sort": "bank"
},
{
"_id": "5be78e34fb6fc06239e0c3b1",
"name": "7Eleven",
"sort": "list"
},
{
"_id": "5be78e5ffb6fc06239e0c3b7",
"name": "Kroger",
"sort": "bank"
}
]
jquery json ajax
jquery json ajax
asked Nov 11 at 2:56
Casey
416
416
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
stores
is the entire response, which is an array, not an individual object you're iterating over, so stores.sort
won't be meaningful. Check the store
property instead:
success: function(stores) {
$.each(stores, function(i, store) {
if (store.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
}
});
}
Or, you might filter
beforehand instead:
success: function(stores) {
const listStores = stores.filter(({ sort }) => sort === 'list');
listStores.forEach(({ name }) => {
$stores.append(`<div><img src="${name}.jpg"></div>`);
});
}
Perfect Thank You!
– Casey
Nov 15 at 1:32
add a comment |
up vote
0
down vote
You can use $.grep
to filter out the objects you want.
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
stores
is the entire response, which is an array, not an individual object you're iterating over, so stores.sort
won't be meaningful. Check the store
property instead:
success: function(stores) {
$.each(stores, function(i, store) {
if (store.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
}
});
}
Or, you might filter
beforehand instead:
success: function(stores) {
const listStores = stores.filter(({ sort }) => sort === 'list');
listStores.forEach(({ name }) => {
$stores.append(`<div><img src="${name}.jpg"></div>`);
});
}
Perfect Thank You!
– Casey
Nov 15 at 1:32
add a comment |
up vote
1
down vote
accepted
stores
is the entire response, which is an array, not an individual object you're iterating over, so stores.sort
won't be meaningful. Check the store
property instead:
success: function(stores) {
$.each(stores, function(i, store) {
if (store.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
}
});
}
Or, you might filter
beforehand instead:
success: function(stores) {
const listStores = stores.filter(({ sort }) => sort === 'list');
listStores.forEach(({ name }) => {
$stores.append(`<div><img src="${name}.jpg"></div>`);
});
}
Perfect Thank You!
– Casey
Nov 15 at 1:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
stores
is the entire response, which is an array, not an individual object you're iterating over, so stores.sort
won't be meaningful. Check the store
property instead:
success: function(stores) {
$.each(stores, function(i, store) {
if (store.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
}
});
}
Or, you might filter
beforehand instead:
success: function(stores) {
const listStores = stores.filter(({ sort }) => sort === 'list');
listStores.forEach(({ name }) => {
$stores.append(`<div><img src="${name}.jpg"></div>`);
});
}
stores
is the entire response, which is an array, not an individual object you're iterating over, so stores.sort
won't be meaningful. Check the store
property instead:
success: function(stores) {
$.each(stores, function(i, store) {
if (store.sort === 'list'){
$stores.append(`<div><img src="${store.name}.jpg"></div>`);
}
});
}
Or, you might filter
beforehand instead:
success: function(stores) {
const listStores = stores.filter(({ sort }) => sort === 'list');
listStores.forEach(({ name }) => {
$stores.append(`<div><img src="${name}.jpg"></div>`);
});
}
answered Nov 11 at 3:01
CertainPerformance
66.6k143152
66.6k143152
Perfect Thank You!
– Casey
Nov 15 at 1:32
add a comment |
Perfect Thank You!
– Casey
Nov 15 at 1:32
Perfect Thank You!
– Casey
Nov 15 at 1:32
Perfect Thank You!
– Casey
Nov 15 at 1:32
add a comment |
up vote
0
down vote
You can use $.grep
to filter out the objects you want.
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
add a comment |
up vote
0
down vote
You can use $.grep
to filter out the objects you want.
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use $.grep
to filter out the objects you want.
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
You can use $.grep
to filter out the objects you want.
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
$(document).ready(function(){
var jsonString = '[{"_id": "5be78df2fb6fc06239e0c39b","name": "Albertsons","sort":"list"},{"_id": "5be78e00fb6fc06239e0c39c","name": "COSTCO","sort": "list"},{"_id": "5be78e17fb6fc06239e0c3ac","name": "Food Lion","sort": "bank"},{"_id": "5be78e34fb6fc06239e0c3b1","name": "7Eleven","sort": "list"},{"_id": "5be78e5ffb6fc06239e0c3b7","name": "Kroger","sort": "bank"}]'
var jsonObject = JSON.parse(jsonString);
// Filter your object using grep
var filteredObjects = $.grep(jsonObject, function(a){
return a.sort == "list"
});
var elements = '';
for(var i = 0; i<filteredObjects.length; i++ ){
elements += '<div>'+filteredObjects[i].name+'</div>'
}
$('#container').append(elements);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
answered Nov 11 at 3:18
SilentCoder
1,84211020
1,84211020
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245456%2ffilter-and-append-ajax-json-result-based-on-objects-with-a-specific-key-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown