Created
October 20, 2016 20:04
-
-
Save victorono/34fa63ae0a98dbe1bbcd869a2b6eef6a to your computer and use it in GitHub Desktop.
Some minor explanation: - We use Array.prototype.filter() function to filter out the keys that start with `imageIds2 - 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment