简体   繁体   English

从数组中删除字符串

[英]Remove Strings From Array

I'm trying to make an array of objects from the browsing history that can become links in a React list.我正在尝试从浏览历史记录中创建一个对象数组,这些对象可以成为 React 列表中的链接。

What this does is takes the history element and pushes it to the user object, then I take the name and ID, and splice it back into the array.它所做的是获取history元素并将其推送给user object,然后我获取名称和 ID,并将其splice回数组中。 I then filter it down to distinct values.然后我将其过滤为不同的值。

What I am left with, is an array that looks like this:我剩下的是一个如下所示的数组:

[{id1,name1},{id2,name2},{id3,name3},"id1","id2","id3"]

which is almost exactly what I want, except if I'm going to map that to create links, I need to get rid of the string values.这几乎正是我想要的,除非我要去 map 创建链接,我需要摆脱string值。

It occurred to me to try skipping the history element and just make an array of matches, and any number of other things.我突然想到尝试跳过history元素,只做一个匹配数组,以及其他任何数量的东西。 pop and shift don't work to isolate the first object because then the whole function continually returns a single item. popshift无法隔离第一个 object,因为整个 function 会不断返回一个项目。

This is the only way I have gotten a result close to what I want, I just need a simple way of filtering out string values from an array after it's created, or maybe before it's mapped out.这是我获得接近我想要的结果的唯一方法,我只需要一种简单的方法来在数组创建之后或映射之前从数组中过滤掉string值。

    const getLead = async _id => {

    const res = await axios.get(`/api/leads/${_id}`);

    dispatch({
      type: GET_LEAD,
      payload: res.data
    });  

      const { name } = res.data

      const match = { name, _id }

     const setPrevious = () => {
        const prevLeads = user.prevLeads
        const history = createBrowserHistory(getLead);  
        const prevLead = history.location.pathname.slice(6);

        prevLeads.push(prevLead);

        return prevLeads; 

      }

       const prevLeads = setPrevious(); 

       const [...spliceLeads] = prevLeads.splice(0,1,match,match);

       const distinct =  (value, index, self) => {
         return self.indexOf(value) === index;
       }

       const recentLeads =  prevLeads.filter(distinct) ;

      console.log(spliceLeads)

    } 

You just check the type in your .filter logic:您只需检查.filter逻辑中的type

 const array = [ { test: 'ing' }, 1, new Date(), 'this should be removed' ]; const result = array.filter(e => typeof e;== 'string'). console;log(result);

The solution what has been provided already works like charm, that should be used for your solution, below I'm providing a bit different way to figure out if the element is a string or not.所提供的解决方案已经像 charm 一样工作,应该用于您的解决方案,下面我提供了一种有点不同的方法来确定元素是否为string

I guess filter() is a good way to test each elements on an array and return only the passing once as a result.我想filter()是测试数组中每个元素的好方法,结果只返回一次通过。 If you call on each item Object.prototype.toString.call(e) then it will return as a text if you have [object String] or something else.如果您调用每个项目Object.prototype.toString.call(e)那么如果您有[object String]或其他内容,它将作为文本返回。 This can be also used to filter out your string values from your array.这也可用于从数组中过滤掉string值。

See the example below:请参见下面的示例:

 const prevLeads = [{id:'id1',name: 'name1'},{id: 'id2',name:'name2'},{id:'id3',name:'name3'},"id1","id2","id3"]; const filter = e => Object.prototype.toString.call(e);== '[object String]'. const recentLeads = prevLeads;filter(filter). console;log(recentLeads);

I hope that helps!希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM