0

I'm trying to make an array of objects from the browsing history that can become links in a React list.

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. 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.

It occurred to me to try skipping the history element and just make an array of matches, and any number of other things. pop and shift don't work to isolate the first object because then the whole function continually returns a single item.

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.

    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)

    } 
1
  • 2
    Use typeof your_string == 'string'?: prevLeads.filter(e => typeof e != 'string') Commented Feb 12, 2020 at 20:34

2 Answers 2

4

You just check the type in your .filter logic:

const array = [
  { test: 'ing' },
  1,
  new Date(),
  'this should be removed'
];

const result = array.filter(e => typeof e !== 'string');
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2

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.

I guess filter() is a good way to test each elements on an array and return only the passing once as a result. 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. This can be also used to filter out your string values from your array.

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.