json 요소 제거
JSON 요소 또는 JSON에서 하나의 전체 행을 제거하고 싶습니다.
다음 JSON 문자열이 있습니다.
{
"result":[
{
"FirstName": "Test1",
"LastName": "User",
},
{
"FirstName": "user",
"LastName": "user",
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
}
]
}
var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.
스플 라이스 를 사용 하여 배열에서 요소를 제거 할 수 있습니다 .
JSON에 후행 쉼표를 사용하지 마십시오.
UPDATE : 객체의 배열에서 항목을 제거하려면 array.splice 를 사용 하고 삭제하지 않아야합니다.
var data = {
"result": [{
"FirstName": "Test1",
"LastName": "User"
}, {
"FirstName": "user",
"LastName": "user"
}]
}
console.log(data.result);
console.log("------------ deleting -------------");
delete data.result[1];
console.log(data.result); // note the "undefined" in the array.
data = {
"result": [{
"FirstName": "Test1",
"LastName": "User"
}, {
"FirstName": "user",
"LastName": "user"
}]
}
console.log(data.result);
console.log("------------ slicing -------------");
var deletedItem = data.result.splice(1,1);
console.log(data.result); // here no problem with undefined.
다음과 같이 JSON 삭제를 시도 할 수 있습니다.
var bleh = {first: '1', second: '2', third:'3'}
alert(bleh.first);
delete bleh.first;
alert(bleh.first);
또는 색인을 전달하여 속성을 삭제할 수도 있습니다.
delete bleh[1];
그러나 삭제 사용의 일부 영향을 이해하려면 여기를 참조하십시오.
- JSON의 오류 수정 : http://jsonlint.com/
- JSON 구문 분석 (자바 스크립트로 질문에 태그를 지정 했으므로 json2.js 사용 )
- 생성 한 개체에서 속성을 삭제 합니다.
- Stringify the object back to JSON.
Try this following
var myJSONObject ={"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
console.log(myJSONObject);
console.log(myJSONObject.ircEvent);
delete myJSONObject.ircEvent
delete myJSONObject.regex
console.log(myJSONObject);
All the answers are great, and it will do what you ask it too, but I believe the best way to delete this, and the best way for the garbage collector (if you are running node.js) is like this:
var json = { <your_imported_json_here> };
var key = "somekey";
json[key] = null;
delete json[key];
This way the garbage collector for node.js
will know that json['somekey']
is no longer required, and will delete it.
As described by @mplungjan, I though it was right. Then right away I click the up rate button. But by following it, I finally got an error.
<script>
var data = {"result":[
{"FirstName":"Test1","LastName":"User","Email":"test@test.com","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
{"FirstName":"user","LastName":"user","Email":"u@u.com","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
{"FirstName":"Ropbert","LastName":"Jones","Email":"Robert@gmail.com","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
{"FirstName":"hitesh","LastName":"prajapti","Email":"h.prajapati@zzz.com","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
]
}
alert(data.result)
delete data.result[3]
alert(data.result)
</script>
Delete is just remove the data, but the 'place' is still there as undefined.
I did this and it works like a charm :
data.result.splice(2,1);
meaning : delete 1 item at position 3 ( because array is counted form 0, then item at no 3 is counted as no 2 )
I recommend splice
method to remove an object from JSON objects array.
jQuery(json).each(function (index){
if(json[index].FirstName == "Test1"){
json.splice(index,1); // This will remove the object that first name equals to Test1
return false; // This will stop the execution of jQuery each loop.
}
});
I use this because when I use delete
method, I get null
object after I do JSON.stringify(json)
try this
json = $.grep(newcurrPayment.paymentTypeInsert, function (el, idx) { return el.FirstName == "Test1" }, true)
참고URL : https://stackoverflow.com/questions/5310304/remove-json-element
'development' 카테고리의 다른 글
git의 use-commit-times와 동등한 것은 무엇입니까? (0) | 2020.08.31 |
---|---|
NSSet에서 객체 가져 오기 (0) | 2020.08.31 |
브라우저 너비 / 높이가 변경 될 때 CSS를 사용하여 이미지의 크기를 동적으로 조정하려면 어떻게해야합니까? (0) | 2020.08.31 |
문자열을 숫자로 변환 (0) | 2020.08.30 |
클래스의 인스턴스 만들기 (0) | 2020.08.30 |