2017-01-04 1 views
-5

.pop() 함수를 사용하여 배열의 마지막 요소를 얻으려고합니다. (웹 페이지에 표시되는 값입니다.) 내가 자바의 배열 .pop()의 마지막 요소를 가져 오는 것이 함수가 아닙니다.

var notif_count = data.pop(); 

내가 오류 ... 자바 스크립트/JQuery와 내가 이것에 조금 어려움을 겪고있어 상당히 새로운 해요 :

내 데이터 "catch되지 않는 형식 오류 data.pop는 함수가 아닙니다" 콘솔에서 다차원 배열처럼 보이지만 잘 모르겠습니다. 다음은 출력의 샘플입니다.

EDIT 2 - 출력은 여기에 표시된 문자가 포함 된 문자열입니다.

[ 
    [ 
    { 
     "_can_chaccess": true, 
     "_can_chown": true, 
     "_can_delete": true, 
     "_can_modify": true, 
     "_can_read": true, 
     "created_at": "Wed Jan 04 17:33:13 UTC 2017", 
     "created_by": 25206, 
     "created_by_user[login_name]": "[email protected]", 
     "created_by_user[person_name]": "Jamie Lowe", 
     "created_through": "Workbooks", 
     "created_through_reference": "", 
     "id": 117662, 
     "imported": false, 
     "key": "notifications", 
     "key10": "", 
     "key11": "", 
     "key12": "", 
     "key13": "", 
     "key14": "", 
     "key15": "", 
     "key16": "", 
     "key17": "", 
     "key18": "", 
     "key19": "", 
     "key2": "292801", 
     "key3": "47930", 
     "key4": "CASE-32109", 
     "key5": "", 
     "key6": "", 
     "key7": "", 
     "key8": "", 
     "key9": "", 
     "lock_version": 0, 
     "owner[login_name]": "[email protected]", 
     "owner[person_name]": "Jamie Lowe", 
     "owner_id": 25206, 
     "type": "Private::Automation::ApiData", 
     "updated_at": "Wed Jan 04 17:33:13 UTC 2017", 
     "updated_by": 25206, 
     "updated_by_user[login_name]": "[email protected]", 
     "updated_by_user[person_name]": "Jamie Lowe", 
     "value": "" 
    }, 
    { 
     "_can_chaccess": true, 
     "_can_chown": true, 
     "_can_delete": true, 
     "_can_modify": true, 
     "_can_read": true, 
     "created_at": "Wed Jan 04 17:23:21 UTC 2017", 
     "created_by": 25206, 
     "created_by_user[login_name]": "[email protected]", 
     "created_by_user[person_name]": "Jamie Lowe", 
     "created_through": "Workbooks", 
     "created_through_reference": "", 
     "id": 117660, 
     "imported": false, 
     "key": "notifications", 
     "key10": "", 
     "key11": "", 
     "key12": "", 
     "key13": "", 
     "key14": "", 
     "key15": "", 
     "key16": "", 
     "key17": "", 
     "key18": "", 
     "key19": "", 
     "key2": "292801", 
     "key3": "47930", 
     "key4": "CASE-32106", 
     "key5": "", 
     "key6": "", 
     "key7": "", 
     "key8": "", 
     "key9": "", 
     "lock_version": 2, 
     "owner[login_name]": "[email protected]", 
     "owner[person_name]": "Jamie Lowe", 
     "owner_id": 25206, 
     "type": "Private::Automation::ApiData", 
     "updated_at": "Wed Jan 04 17:30:39 UTC 2017", 
     "updated_by": 25206, 
     "updated_by_user[login_name]": "[email protected]", 
     "updated_by_user[person_name]": "Jamie Lowe", 
     "value": "" 
    } 
    ], 
    2 
] 

기본적으로 내가 바로 닫기 전에 배열의 끝에 숫자 2를 얻으려고] - 내가 어떻게해야합니까?

편집 : FULL 코드 예

var notif_count = data[1]; <!-- Last element in the array which is the count of notifications calculated server side by PHP -->     
       console.log(notif_count);      
       $('#count').text(notif_count); 
+1

[재현 할 수없는 (http://imgs.xkcd.com/comics/cnr.png) (크롬, FF). – ASDFGerte

+1

방금 ​​배열을 내 콘솔에 붙여 넣고'.pop()'를 성공적으로 호출했습니다. 'pop '이라고 부르는 데 사용하는 코드를 보여주세요 – Michael

+0

이 여기에 있습니다 - [예제] (http://jsbin.com/kotadanigo/edit?js,console) –

답변

0

를 사용하여 배열 인덱싱. JS의 배열에 대한 자세한 내용은 MDN을 확인하십시오.

문제에 대한

,이

var notif_count = data[1] 
//index 1 will give 2nd element, as array indexing is zero indexing. 

그리고 나를 위해 var notif_count = data.pop(); 작품을 워크 것이다. FF 콘솔에서 확인되었습니다. 차이점이없는 동일한 예.

JSON 문자열의 줄 바꿈 문자를주의하십시오.

0

댓글에 달린 의견에 따라 편집되었습니다. 먼저리스트로 문자열을 변환하는 JSON.parse를 사용할 필요가 다음 인덱스의 마지막 항목을 선택합니다

var data = '["a","b","c"]'; 
data_2 = JSON.parse(data) 
var notif_count = data_2[data_2.length-1]; 
console.log(notif_count) --> 'c' 
console.log(data_2) --> ['a','b','c'] 
+0

불행히도 이것은 나를 위해 작동하지 않습니다 ... 나는 아마도 배열과 같이 보이는 출력과 관련이 있다고 생각합니다. 실제로는 문자열입니다. 배열로 다시 변환하려면 어떻게해야합니까? –

+0

위의 주석을 보려면 JSON.parse()를 먼저 사용하십시오. – oli5679

관련 문제