Js knows an array of value values and filters out an array of corresponding text values based on dictionary relational data.

var values = ["1","2","4"];
var data = [{value:"1",text:"1"},
            {value:"2",text:"2"},
            {value:"3",text:"3"},
            {value:"4",text:"4"},
            {value:"5",text:"5"},
            {value:"6",text:"6"}];
            
 valuestext
:var texts = ["1","2","4"];

Code wants to be concise and efficient

Sep.28,2021

     var data = [{value:'1',text:'1'},
        {value:'2',text:'2'},
        {value:'3',text:'3'},
        {value:'4',text:'4'},
        {value:'5',text:'5'},
        {value:'6',text:'6'}];
     var arr = ['1','2','4']
     var arr1 = []
        data.map(e=>{
            if(arr.includes(e.value))arr1.push(e.text)
        })
        console.log(arr1)
        

clipboard.png


// :filtermap includes ES7indexOf
var data = [{value:'1',text:'1'},
    {value:'2',text:'2'},
    {value:'3',text:'3'},
    {value:'4',text:'4'},
    {value:'5',text:'5'},
    {value:'6',text:'6'}];
var arr = ['1','2','4']
var result = data.filter(e=>{
    return arr.includes(e.value)
}).map(el => el.text);
console.log(result); // ["1", "2", "4"]

var data = [{ value: '1', text: '1' },
  { value: '2', text: '2' },
  { value: '3', text: '3' },
  { value: '4', text: '4' },
  { value: '5', text: '5' },
  { value: '6', text: '6' }];
  var arr = ['1', '2', '4']
  let newArr=data.reduce((prev,cur)=>{
    arr.includes(cur.value)&&prev.push(cur.text)
    return prev
  },[])
  console.log(newArr); //(3)["1", "2", "4"]

function test() {
  var values = ['1','2','4'];
  var data = [{value:'1',text:'1'},
            {value:'2',text:'2'},
            {value:'3',text:'3'},
            {value:'4',text:'4'},
            {value:'5',text:'5'},
            {value:'6',text:'6'}];
  let result = new Map(data.map(key=>[key.value, key.text]))

  let temp = new Set()
  values.map((item)=>{
    if (result.has(item)) {
      temp.add(result.get(item))
    }
  })
  return [...temp.values()]
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b19d-2c295.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b19d-2c295.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?