2016-05-13 171 views
-2

喜的朋友我想合併兩個JSON數組我試圖CONCAT和合並的方法,但它沒有給出正確的輸出請建議什麼......將數組傳遞給查詢參數

var set_image=[{"id":"aerobics"},{"id":"kick boxing"}] 
var item_json=[{"id":"net ball"},{"id":"floor ball"}] 

合併完畢數組

var finalArray =[{"id":"aerobics"},{"id":"kick boxing"},{"id":"net ball"},{"id":"floor ball"}] 

這裏是我的javascript

var item = JSON.parse(localStorage.getItem("test")); 
var item_json = JSON.stringify(item) ; 

var page= <?php echo $json_value; ?>; 
var set_image=JSON.stringify(page) ; 


//var image=set_image.concat(item_json); 
var image= $.concat(set_image, item_json) 


window.location.href = "modal.php?ids=" + image; 
+2

這看起來究竟是concat應該如何。你能指望什麼? –

+0

當本地JS方法存在時,不需要使用jQuery concat。 – evolutionxbox

+0

@RegisPortalez我想合併兩個數組concat方法是不合並 –

回答

1

轉換的合併數組JSON字符串後ENCOD Ë它使用encodeURIComponent()在URL傳遞

var set_image=[{"id":"aerobics"},{"id":"kick boxing"}] 
 
var item_json=[{"id":"net ball"},{"id":"floor ball"}] 
 

 
var arr= set_image.concat(item_json); 
 

 
window.location.href = "modal.php?ids=" + encodeURIComponent(JSON.stringify(arr));

+0

我的網址就像這樣http://localhost/carc_app/user/modal.php?id =%5B%22%5B%7B%5C%22id%5C%22%3A%5C%22yoga%5C%22% 7D%2C%7B%5C%22id%5C%22%3A%5C%22aerobics%5C%22%7D%5D%22%2C%22%5B%7B%5C%22id%5C%22%3A%5C% 22%%20球%5C%22%7D%2C%7B%5C%22%5C%22%3A%5C%22地板%20球%5C%22%7D%5D%22%5D –

+0

10 @Rahul:json字符串編碼爲合格在url中...現在你可以解析它從服務器...''json_decode($ _ GET ['ids']);' –

0

concat()是你所需要的東西,但是你對它的使用是不正確:

var set_image = [{ "id": "aerobics" },{ "id": "kick boxing" }] 
var item_json = [{ "id": "net ball" },{ "id": "floor ball" }] 
var image = set_image.concat(item_json); 

Working example

另請注意,你的最後一行a將image數組掛接到字符串將不會產生所需的效果,因爲它只會多次添加[object Object]。我猜你會想要循環訪問數組,並逐個追加每個值id

+0

這一個我已經嘗試但不合並.. –

+0

你確定,因爲它工作絕對好: https://jsfiddle.net/q5e83jud/ –

+0

你的回答是正確的,但它仍然像這樣[{「id」:「yoga」},{「id」:「aerobics」}],[{「id」: 「net ball」},{「id」:「floor ball」}] –