0

i tried this function in javascript :

var words = $('textarea#id_keywords').val();
$('#rangeVal').change(function(){
  console.log(this.value);

  console.log(words);
  $('input#id_wordcloud3_keywords').val('toto');
  $('input#id_wordcloud3_keywords').trigger('change');
});

with var words contains :

 {'type': 'new', 'entity': [], 'w': [{'text': 'balabala', 'relevance': 1, 'index': 0.0010035}, {'text': 'numéro', 'relevance': 1, 'index': 0.14269}, {'text': 'majeur', 'relevance': 1, 'index': 0.0886}]}

so i tried to sort the array according to index :

var words = $('textarea#id_keywords').val();
$('#rangeVal').change(function(){
  console.log(this.value);
  //console.log(words);

  var tableauObjets = words['w'];
  var result = tableauObjets.filter(x => x['index'] <= this.value);

  console.log(result);
  
});

but i have an error:

 uncaught TypeError: Cannot read property 'filter' of undefined at HTMLInputElement.<anonymous>
  • i think that the problem in "var words = $('textarea#id_keywords').val();" we cannot use this in " var tableauObjets = words['w'] " like this .... i am beginner in javascript ... – John nicola Dec 08 '20 at 21:52
  • words is a string.... unclear why you are using bracket notation with it. I assume you want to take the value inside of the input and convert it into an actual object. – epascarello Dec 08 '20 at 22:11
  • yes exactly, how we can do this ? – John nicola Dec 08 '20 at 22:18
  • Well the fact it is not valid JSON means you would have to use eval – epascarello Dec 08 '20 at 22:19
  • i dot understand; can tou show me an example how we can do this, im beginner on javascript – John nicola Dec 08 '20 at 22:22
  • https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – epascarello Dec 08 '20 at 22:24
  • i tried JSON.parse(words) but i have a problem because in words the keys are between single quotes not double quotes , how we can change the encode of quotes to double quotes ? – John nicola Dec 08 '20 at 22:52

0 Answers0