Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: How can I get all the keys and values of an object that begin with a specific string?

Tags:

javascript

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

My object appears as the following:

{
    title: 'fsdfsd',
    titleZh: 'fsdfsd',
    body: 'fsdf',
    bodyZh: 'sdfsdf',
    imageIds: '/uploads/tmp/image-3.png',
    imageIdsZh: '' 
}

but I only need the properties imageIds, and imageIdsZh. However tomorrow, a object might contain imageIdsBlah and I would need to pick it up as well. I could remove the first few properties from the object, but then the next object might contain additional properties such as foo: 'bar'

like image 292
Neil Avatar asked Dec 10 '13 03:12

Neil


People also ask

Which method is useful to get all values of object in a string form?

values() Method: The Object. values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object.

How do I get all the properties of an object?

To get all own properties of an object in JavaScript, you can use the Object. getOwnPropertyNames() method. This method returns an array containing all the names of the enumerable and non-enumerable own properties found directly on the object passed in as an argument.


3 Answers

Some functional style awesomeness:

var data = {
    title: 'fsdfsd',
    titleZh: 'fsdfsd',
    body: 'fsdf',
    bodyZh: 'sdfsdf',
    imageIds: '/uploads/tmp/image-3.png',
    imageIdsZh: '' 
};

var z = Object.keys(data).filter(function(k) {
    return k.indexOf('imageIds') == 0;
}).reduce(function(newData, k) {
    newData[k] = data[k];
    return newData;
}, {});

console.log(z);

Demo: http://jsfiddle.net/ngX4m/

Some minor explanation:

  1. We use Array.prototype.filter() function to filter out the keys that start with `imageIds2
  2. We use Array.prototype.reduce() to convert an array of filtered keys into an object of key-value pairs. For that we use the initial value of {} (an empty object), fill it and return from every execution step.

UPD:

A fair update from @GitaarLAB:

Object.keys is ES5, but returns an objects own properties (so no need for obj.hasOwnProperty(key))

like image 189
zerkms Avatar answered Oct 14 '22 22:10

zerkms


Depending on how large the object is, this may not scale well, but it will work

for(key in obj){
   if(obj.hasOwnProperty(key)){
      if(key.indexOf("imageIds")===0){
        //do something
      }
   }
}
like image 44
scrblnrd3 Avatar answered Oct 15 '22 00:10

scrblnrd3


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.

like image 22
Scott Sauyet Avatar answered Oct 15 '22 00:10

Scott Sauyet