分割字符串,然后合并并删除数组中的重复值。

huangapple go评论57阅读模式
英文:

Split string then merge and remove duplicate value in array

问题

I have an API and retrieve the data using jQuery.JSON. I use split to split the locations by "|" (pipe). And now I'm trying to merge arrays that I've split using each in jQuery and remove the duplicates. I already tried concat. Is there an array_merge and array_filter function for JavaScript/jQuery?

Here is my sample code below.

jQuery(document).ready(function($) {
  let get_json = 'https://boards-api.greenhouse.io/v1/boards/frequence/departments/';
  $.getJSON(get_json, function(data) {
    let dept_arr = new Array();
    let arr = new Array();
    let i = 0;
    $.each(data.departments, function(key, value) {
      if (value.jobs.length > 0) {
        $.each(value.jobs, function(key, value) {
          dept_arr[i] = (value.location.name.split('|'));
          i++;
        });
      }
    });
    console.log(dept_arr);
  });
});
英文:

I have an API and retrieve the data using jQuery.JSON. I use split to split the locations by "|". And now I'm trying to merge arrays that I've split using each in jQuery and remove the duplicates. I already tried concat. Is there a array_merge then array_filter function for javascript/jquery?

Here is my sample code below.

jQuery(document).ready(function($) {
  let get_json = 'https://boards-api.greenhouse.io/v1/boards/frequence/departments/';
  $.getJSON(get_json, function(data) {
    let dept_arr = new Array();
    let arr = new Array();
    let i = 0;
    $.each(data.departments, function(key, value) {
      if (value.jobs.length > 0) {
        $.each(value.jobs, function(key, value) {
          dept_arr[i] = (value.location.name.split('|'));
          i++;
        });
      }
    });
    console.log(dept_arr);
  });
});

答案1

得分: 1

你需要循环遍历split()返回的数组中的所有元素。最简单的方法是使用Set来去除重复项。

jQuery(document).ready(function($) {
  let get_json = 'https://boards-api.greenhouse.io/v1/boards/frequence/departments/';
  $.getJSON(get_json, function(data) {
    let dept_set = new Set();
    $.each(data.departments, function(key, dept) {
      $.each(dept.jobs, function(key, job) {
        let locations = job.location.name.split('|');
        $.each(locations, (i, loc) => dept_set.add(loc));
      });
    });
    let dept_arr = [...dept_set]; // convert set to array
    console.log(dept_arr);
  });
});
英文:

You need to loop over all the elements in the array returned by split(). And the easiest way to get rid of duplicates is with a Set.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

jQuery(document).ready(function($) {
  let get_json = &#39;https://boards-api.greenhouse.io/v1/boards/frequence/departments/&#39;;
  $.getJSON(get_json, function(data) {
    let dept_set = new Set();
    $.each(data.departments, function(key, dept) {
      $.each(dept.jobs, function(key, job) {
        let locations = job.location.name.split(&#39;|&#39;);
        $.each(locations, (i, loc) =&gt; dept_set.add(loc));
      });
    });
    let dept_arr = [...dept_set]; // convert set to array
    console.log(dept_arr);
  });
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 07:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定