我的JSON对象数组是:
#include <stdio.h>
#include <string.h>
int main()
{
char string_buffer[21];
int i, length;
int flag = 0;
print f("Input a string\n");
scanf("%20s", string_buffer);
length = strlen(string_buffer);
for(i=0; i < length; i++) {
if(string_buffer[i] != string_buffer[length-i-1]) {
flag = 1;
break;
}
}
if(flag) {
printf("%sNot a Palindrome.\n");
}
else {
printf("%sPalindrome.\n");
}
return 0;
}
我想创建一个新的JSON对象数组,这些对象应该只包含上面数组[{
"stateCd": "IL",
"value": "Illinois",
"selected": "false"
},
{
"stateCd": "CA",
"value": "California",
"selected": "true"
},
{
"stateCd": "NY",
"value": "New york",
"selected": "false"
}]
的那些对象。
我试过selected:false
:
concat
但是这会将angular.forEach($scope.oldJsonArr, function (value, index) {
if($scope.oldJsonArr[index].selected=="false"){
$scope.newJsonArr.concat($scope.oldJsonArr[index]);
}
});
作为未定义返回。
任何帮助表示赞赏!
答案 0 :(得分:2)
以下是代码:
$scope.oldJsonArr = [{"stateCd": "IL", "value": "Illinois", "selected": "false"}, {
"stateCd": "CA",
"value": "California",
"selected": "true"
}, {"stateCd": "NY", "value": "New york", "selected": "false"}];
$scope.newJsonArr = [];
angular.forEach($scope.oldJsonArr, function (value, index) {
if ($scope.oldJsonArr[index].selected == "false") {
$scope.newJsonArr.push($scope.oldJsonArr[index]);
}
});
console.info($scope.newJsonArr);
undefined
.push()
方法答案 1 :(得分:0)
您可以使用Array.prototype.filter()方法。
var falseFilter = function(value) {
return value.selected == "false";
};
var newJsonArray = oldJsonArray.filter(falseFilter);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
答案 2 :(得分:0)
使用Filter
中的JavaScript
,而不是使用库实用程序功能。
var people = [
{ firstname:"Micro", hasSocialNetworkSite: false, lastname:"Soft", site:"http://microsoft.com" },
{ firstname:"Face", hasSocialNetworkSite: true, lastname:"Book", site:"http://facebook.com" },
{ firstname:"Go", hasSocialNetworkSite: true, lastname:"ogle", site:"http://google.com" },
{ firstname:"Twit", hasSocialNetworkSite: true, lastname:"Ter", site:"http://twitter.com" },
{ firstname:"App", hasSocialNetworkSite: false, lastname:"Le", site:"http://apple.com" },
{ firstname:"Master", hasSocialNetworkSite: false, lastname:"Card", site:"http://mastercard.com" }
];
var filteredResult = people.filter(function(v) {
return v.hasSocialNetworkSite == true; // Filter out the appropriate one
});
document.write("Social Networking Site<br>");
for (var i in filteredResult) {
document.write(filteredResult[i].site + "<br>")
}
参考资料详细了解:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
https://msdn.microsoft.com/en-us/library/ff679973(v=vs.94).aspx
http://www.w3schools.com/jsref/jsref_filter.asp