0

내 Laravel 응용 프로그램에서 Material Design Tags을 사용하고 있습니다.재료 디자인 부트 스트랩 태그 초기화

<div class="chips chips-placeholder chips-initial"></div> 

나는 데이터베이스에 저장된 값이 칩 요소를 초기화 할 :

array:1 [▼ 
    0 => "art" 
] 

가 어떻게 할당 할 수 있습니다 다음과 같은

["kingfisher", "art"] 

그리고

이 날에 내가 $art->keywords 받고 있어요 이 값을 jQuery 코드 아래로?

var keywords[] = "{{ json_encode($art->keyword) }}"; 
console.log(keywords); 
$.each(keywords, function(index, value) { 
    alert(index + ": " + value); 
}) 

그러나 나는 다음과 같은 오류받을 :이 오류를 잡

var keywords = "{{ json_encode($art->keyword) }}"; 
$.each(JSON.parse(keywords), function(index, value) { 
    alert(index + ": " + value); 
}); 

: 나는 또한이 시도

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in ["art"]

$('.chips-initial').material_chip({ 
    data: [{ 
     tag: 'Tag 1', 
    }, { 
     tag: 'Tag 2', 
    }, { 
     tag: 'Tag 3', 
    }], 
    }); 

나는이 시도배열이 숫자 즉,이 경우

Uncaught SyntaxError: Unexpected token & in JSON at position 1

모든 것이 괜찮 [1,2,3]

답변

0
var keywords = '{{ json_encode($art->keyword) }}'; 
if(keywords != 'null'){ 
    var data = JSON.parse(keywords.replace(/&quot;/g,'"')); 
    var option = {}; // my object 
    var options = []; // my array 
    $.each(data, function(index, value) { 
     option = { 
      tag : value 
     } 
     options.push(option); 
    }); 
    console.log(options); 
    $('.chips-initial').material_chip({ 
     data: options, 
    }); 
}