{
"images": [
{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [
{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x" : [12,345],
"y": "234"
},
"long": 123
}
}
}
}
}
]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
My main goal is to loop through this and return a new object containing items that have at least one digit so in the end i will have something similar to this example. The new object should be a new JSON file:
{
"key": "ASDV1-01.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F10": "101076",
}
}
Of course, this should be applied for the whole object. Tried to do this with regex syntax but with no use. Thank you in advance
CodePudding user response:
By digit you mean like 0 or a? (then it would be a character) you can loop trough the whole Json and check for each key if the corresponding value is len() >= 1 if it is, then add it to a new Object. You can also use a filter like in this example (example for getting all odd numbers):
const odd = x => x % 2 === 1;
[1, 2, 3].filter(x => x % 2 === 1);
Have a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
CodePudding user response:
loop through whole array of images and test() key if it contains any digit with /\d/
regex
let data = [
{ "key": "no-digit.jpg" },
{ "key": "has2-digits7.jpg" },
{ "key": "again-no-digit.jpg" }
];
data = data.filter(item => /\d/.test(item.key));
console.log(data);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>