Select items that have at least one digit from object in javascript - javascript

{
"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

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

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);

Related

Find every possibilitie in an array of object comparing key values

I have an specific math formula which receives three parameters and I'm trying to get from an array of object all the possibilities to run this math formula.
It's regards to sports.
Imagine a match where there are 3 possibility: Team A (win) - Team B (win) - Draw.
3 bet websites are dealing with this event. But the 3 of them have different odds values for this match.
I want to run those 3 bet websites to get all posibilities I can have for this event. Never getting more than one odd from the same bet website.
Example:
Website A: team A (win)
Website B: team B (win)
Website C: draw
I'm using JavaScript for that.
Thank you in advance for you time and support.
Really appreciate that.
Here is an example of data I have to get these possibilities.
Each obj is a website and into the object, the odds are on the key "outcomes".
The array of object here has 3 objects, but it can have more
[
{
"key": "betmgm",
"title": "BetMGM",
"last_update": "2022-12-14T04:30:40Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 138
},
{
"name": "Tottenham Hotspur",
"price": 200
},
{
"name": "Draw",
"price": 225
}
]
}
]
},
{
"key": "barstool",
"title": "Barstool Sportsbook",
"last_update": "2022-12-14T04:30:22Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 130
},
{
"name": "Tottenham Hotspur",
"price": 220
},
{
"name": "Draw",
"price": 230
}
]
}
]
},
{
"key": "twinspires",
"title": "TwinSpires",
"last_update": "2022-12-14T04:17:45Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 130
},
{
"name": "Tottenham Hotspur",
"price": 220
},
{
"name": "Draw",
"price": 230
}
]
}
]
}
]
I would like to receive an array with the possibilities like this:
[
{
"bookMaker": "TwinSpires",
"name": "Tottenham Hotspur",
"price": 230,
},
{
"bookMaker": "Barstool Sportsbook",
"name": "AC Milan",
"price": 130,
},
{
"bookMaker": "BetMGM",
"name": "Draw",
"price": 225,
}
]
The algorithm is quite simple. First, we take all the outcomes of the first Bet website. We shuffle them randomly.
Then we simply loop through the JSON object, which is a list of bet websites, not to mention the fact that both the number of outcomes must be the same as the number of bet websites. We assign for each bet website the next item of the shuffled outcomes. We log it to the console.
Here is the algorithm:
var data = [
{
"key": "betmgm",
"title": "BetMGM",
"last_update": "2022-12-14T04:30:40Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 138
},
{
"name": "Tottenham Hotspur",
"price": 200
},
{
"name": "Draw",
"price": 225
}
]
}
]
},
{
"key": "barstool",
"title": "Barstool Sportsbook",
"last_update": "2022-12-14T04:30:22Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 130
},
{
"name": "Tottenham Hotspur",
"price": 220
},
{
"name": "Draw",
"price": 230
}
]
}
]
},
{
"key": "twinspires",
"title": "TwinSpires",
"last_update": "2022-12-14T04:17:45Z",
"markets": [
{
"key": "h2h",
"outcomes": [
{
"name": "AC Milan",
"price": 130
},
{
"name": "Tottenham Hotspur",
"price": 220
},
{
"name": "Draw",
"price": 230
}
]
}
]
}
];
start(data);
function start(data) {
var outcomes = data[0].markets[0].outcomes;
if (data.length != outcomes.length) {
alert("Invalid data!");
return;
}
var results = createResults(data);
printResults(results);
}
function createResults(data) {
var outcomes = data[0].markets[0].outcomes;
var newOutcomesNames = shuffleOutcomes(outcomes);
var results = [];
for (var i = 0; i < data.length; i++) {
var currentBetWebsite = data[i];
var currentResult = {};
var currentOutcomes = currentBetWebsite.markets[0].outcomes;
currentResult.bookMaker = currentBetWebsite.title;
currentResult.name = newOutcomesNames[i];
for (var j = 0; j < newOutcomesNames.length; j++) {
if (newOutcomesNames[i] == currentOutcomes[j].name) {
currentResult.price = currentOutcomes[j].price;
}
}
results.push(currentResult);
}
return results;
}
function printResults(results) {
console.log(results);
}
function shuffleOutcomes(outcomes) {
var parsedOutcomes = parseOutcomesNames(outcomes);
var outcomesLength = parsedOutcomes.length;
var newOutcomes = [];
for (var i = 0; i < outcomesLength; i++) {
var random = Math.floor(Math.random() * (outcomesLength - i));
newOutcomes.push(parsedOutcomes[random]);
parsedOutcomes.splice(random, 1);
}
return newOutcomes;
}
function parseOutcomesNames(outcomes) {
var outcomesNames = outcomes.map(o => o.name);
return outcomesNames;
}
Let me know, if you need any further assistance.

Can't loop array after grouping with .reduce

I've got the following meetings object :
[
{
"id": 19,
"duration": 1.75,
"Employee": {
"name": "Jeanne",
}
},
{
"id": 20,
"duration": 1.00,
"Employee": {
"name": "Louis",
}
},
{
"id": 21,
"duration": 1.00,
"Employee": {
"name": "Jeanne",
}
}
]
I want to group it by Employee.name. Using reduce() here is what I come up with :
meetings.reduce(function (r, a) {
r[a.Employee.name] = r[a.Employee.name] || [];
r[a.Employee.name].push(a);
return r;
}
The resulting object is the following :
{
"Jeanne": [
{
"id": 19,
"duration": 1.75,
"Employee": {
"name": "Jeanne"
}
},
{
"id": 21,
"duration": 1.00,
"Employee": {
"name": "Jeanne"
}
}
],
"Louis": [
{
"id": 20,
"duration": 1.00,
"Employee": {
"name": "Louis"
}
}
]
}
If I try to map() or forEach() i cannot get the value of the element :
Array.from(thisMeeting).forEach(element => console.log(element));
return `undefined`;
Array.from-ming an Object will result in an empty array.
You'll have to iterate over the objects keys with Object.entries(thisMeeting).forEach… and grab the values inside that.

how to modify properties of an nested object?

I have the following nested object and I need to leave the "alias" property blank and the "group" property set to true for all "entries" and "exits". I also need to delete the whole "parameters" object.
Would there be a way to do it all in one function? I've tried to apply the delete Object method but it doesn't work as it's an indexed object.
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "do",
"group": false
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "re",
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3,
"group": false
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": "do"
"group": false
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": "re"
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3
"alias": "mi"
"group": false
}
},
"order": 2
}
}
}
the desired nested object would be the following
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "",
"group": true
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "",
"group": true
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": ""
"group": true
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": ""
"group": true
}
},
"order": 2
}
}
}
what I've tried is the following, managing to delete the "parameters" object but I can't access the "label" property of each "entry" and "exit
const nedtedObjectsValues = Object.values(nestedObjects);
for (object of nedtedObjectsValues) {
delete object.properties.parameters;
}
if anyone can give me an idea of how to approach this function.
Thank you in advance.
In JavaScript, to reference numeric object properties, you need to use the square brackets syntax:
object.1 // bad
object[1] // good
You can delete numeric property like this:
delete object[1];

Fuzzy search deep array only on certain properties

I have a JSON dataset which could be very large when it returns, with the following structure for each object:
{
"ctr": 57,
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}
Now I want to search all objects in the array and return only objects which include a string of some sort, but I only want to search certain properties.
I basically have another array which contains the keys I want to search, e.g.
const iteratees = ['ctr', 'property.value', 'status.stage']
I have lodash available within the project, but I have no idea where to start.
Any ideas?
You could use filter(), some() and reduce() to do this.
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if(a) return a[b];
}, o);
if(res) {
if((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
var arr = [{
"ctr": 'lorem',
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}, {
name: 'lorem',
ctr: 12,
property: {
value: 1
},
status: {
stage: 1
}
}, {
name: 'ipsum'
}]
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if (a) return a[b];
}, o);
if (res) {
if ((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
console.log(result)

Strange values with bar stack Open Flash Chart

I'm trying to use the bar stack graph with Open Flash Chart. The JSON object I generate is containing correct values and when the chart is loaded the first part of the bar stack is correct but the ones after have gotten way of values.
The JSON object:
{
"title": {
"text": "Aktiviteter",
"style": "{font-size: 15px; text-align: center;}"
},
"elements": [
{
"type": "bar_stack",
"colours": [
"#b727e3",
"#f55225",
"#16196e",
"#b3ac3",
"#f01673"
],
"keys": [
{
"colour": "#b727e3",
"text": "Utst\u00e4llningar",
"font-size": 13
},
{
"colour": "#f55225",
"text": "Kurser",
"font-size": 13
},
{
"colour": "#16196e",
"text": "Resor",
"font-size": 13
},
{
"colour": "#b3ac3",
"text": "Kulturarrangemang",
"font-size": 13
},
{
"colour": "#f01673",
"text": "F\u00f6rel\u00e4sningar",
"font-size": 13
}
],
"values": [
[
{
"val": "205",
"tip": "Utst\u00e4llningar 2013<br>Antal #val#"
},
{
"val": "52",
"tip": "Kurser 2013<br>Antal #val#"
},
{
"val": "161",
"tip": "Resor 2013<br>Antal #val#"
},
{
"val": "123",
"tip": "Kulturarrangemang 2013<br>Antal #val#"
},
{
"val": "123",
"tip": "F\u00f6rel\u00e4sningar 2013<br>Antal #val#"
}
]
],
"on-show": {
"type": "pop",
"cascade": 1,
"delay": 0.5
}
}
],
"x_axis": {
"labels": {
"rotate": 20,
"labels": [
"2013"
]
}
},
"y_axis": {
"min": 0,
"steps": 66,
"max": 830
},
"tooltip": {
"mouse": 2
},
"bg_colour": "#FFFFFF"
}
Item #2:
{
"val": "52",
"tip": "Kurser 2013<br>Antal #val#"
},
, when displayed, got a value of 20 347 instead of 52 and therefore doesnt fit into the y-axis. (image http://s8.postimg.org/jfeh9u2d1/Namnlo_st.png)
I load the Chart by
<script type="text/javascript" src="js/js/swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF(
"open-flash-chart.swf", "my_chart", "550", "200",
"9.0.0", "expressInstall.swf",
{"data-file":"get_stats2.php?action=activities%26type=total%26limit=1"}
);
Any suggestions?

Categories

Resources