[removed] How can I get all the keys and values of an object that begin with a specific string?

前端 未结 5 1703
一整个雨季
一整个雨季 2021-01-04 03:56

I am trying to get all the keys and values of an object that begin with imageIds.

My object appears as the following:

{
    title: \'fsd         


        
5条回答
  •  长情又很酷
    2021-01-04 04:33

    Along the lines of the answer from @zerkms, I've been working on a functional programming library for Javascript. With the tools from that library, this turns into a (somewhat dense) one-liner:

    var data = {
        title: 'fsdfsd',
        titleZh: 'fsdfsd',
        body: 'fsdf',
        bodyZh: 'sdfsdf',
        imageIds: '/uploads/tmp/image-3.png',
        imageIdsZh: '' 
    };
    
    var x = pick(filter(compose(eq("imageIds"), substring(0,8)), keys(data)), data);
    console.log(x);
    

    This code is not necessarily any better than what @zerkms posted, but it does show off some more of the power of functional abstractions beyond the few that are built into Array.prototype.

    You can see it in action on JSFiddle.

提交回复
热议问题