_.findWhere array-object within array-object property equals something - mapping

I have an array like this:
var colors = [
{
"name": "red",
"ids":[
{
"id": 1
}
]
},
{
"name": "blue",
"ids":[
{
"id": 5
}
]
}
]
And I essentially want to find the object where the first id within ids is equal to something.
_.findWhere(colors, {
"ids.0.id": 1
})
Is this the best way to go about it?
var color = _.chain(colors)
.map(function(color){
color.id = color.ids[0].id
return color
})
.findWhere({
"id": 1
})
.value()
console.log(color)

_.findWhere is just a convenience wrapper for _.find so if findWhere doesn't do quite what you need, go straight to find and do it by hand:
var color = _(colors).find(function(color) {
return color.ids[0].id === 1;
});

Related

Convert and Sort Data for HighChart Column Range Representation

I have some data coming in random order and would like to convert into into a specific order for Highchart column ranges. Any insight on doing this effectively and insight would help
Also regardless of order of input data I always want to show chart in Apple Orange Banana order with their correct representation
I have tried using maps,sets,array in ruby and have something working which is super brittle and not the most effective.
headers = Array.wrap(raw_data.dig('data', 'dimensions', 'axes', 'headers'))
values = Array.wrap(raw_data.dig('data', 'values', 'c')).map(&:to_f)
labels = headers.map { |header| Array.wrap(header['label']) }
data = values.each_slice(2)
This is the weight of the fruits LOW is lowest weight and HIGH is highest weight. The problem is order of data is ordered by weight so I cant just slice consecutive values of array.
JSON DATA
{
"data": {
"dimensions": {
"axes": {
"headers": [{
"label": ["Apple", "Low"]
}, {
"label": ["Apple", "High"]
}, {
"label": ["Orange", "Low"]
}, {
"label": ["Banana", "Low"]
}, {
"label": ["Orange", "High"]
}, {
"label": ["Banana", "High"]
}]
}
}
"values": {
"c": ["173", "273", "414", "608", "610", "1050"]
}
}
EXPECTED OUTPUT
{
series: [
{'name': 'Weight', 'data': [[173, 273], [414, 610], [608, 1050]]}
],
axis_labels: ['Apple', 'Orange', 'Banana'],
}
chart
https://jsfiddle.net/Praveen2710/7sdqz6Le/8/
You need to preprocess your data to the format required by Highcharts:
var json = {...}
var series = {
name: 'Weight',
data: []
},
i,
header1,
header2,
value,
indexOf,
point,
categories = [];
for (i = 0; i < json.data.values.c.length; i++) {
labels = json.data.dimensions.axes.headers[i].label;
header1 = labels[0].toLowerCase(),
header2 = labels[1].toLowerCase(),
value = json.data.values.c[i];
indexOf = categories.indexOf(header1);
if (indexOf !== -1) {
series.data[indexOf][header2] = Number(value);
} else {
categories.push(header1);
series.data.push({
[header2]: Number(value),
x: series.data.length
});
}
}
Highcharts.chart('container', {
...,
series: [series]
});
Live demo: http://jsfiddle.net/BlackLabel/nm976qho/

Add bpmn:Resource in propertiesPanel

Is it possible to add "bpmn:Resource" element in propertiesPanel and edit its attribute? how to do it?
I've already added some properties to property panel based on the property-panel[link] example.
But I have a challenge in adding 'bpmn:Resource' to the properties panel. I don't want it to be added as an extensionElement.
I'd like it to be inside bpmn:Definitions (beside bpmn:Process). Also I'd like to extend the original bpmn:Resource to add some parameters.
So in MyModdle.json I added:
{
"name": "Resource",
"extends": [
"bpmn:Resource"
],
"properties": [
{
"name": "parameters",
"isMany": true,
"type": "MyParameter"
}
]
}, {
"name": "MyParameter",
"properties": [
{
"name": "myParameterType",
"isAttr": true,
"type": "String"
}
{
"name": "myParameterName",
"isAttr": true,
"type": "String"
},
{
"name": "myParameterValue",
"isAttr": true,
"type": "String"
}
]
}
now for example in newElement function of MyResource.js
var newElement = function (type, prop, factory) {
return function (element, extensionElements, value) {
var commands = [];
var resource = getResource(element);
if (!resource) {
var parent = extensionElements;
resource = createResource(parent, bpmnFactory);
console.log('resource', resource);
commands.push(cmdHelper.addAndRemoveElementsFromList(
element,
extensionElements,
'values',
'extensionElements',
[resource],
[]
));
}
var newElem = createResourceParameter(type, resource, bpmnFactory, {
resourceId: 'id-' + value
});
commands.push(cmdHelper.addElementsTolist(element, parameters, prop, [newElem]));
return commands;
};
}
I know this cmdHelper adds 'bpmn:Resource' to extensionElements but I don't know what to use instead!

How to search JSON element using SwiftyJSON in Swift 3

I search around here and little example to show how to search a json element in SwiftyJSON. I have below json structure, can anyone tech me? thanks.
var jsonData = {
"css":[
{
"path": "style.css",
"updated": "12432"
},
{
"path": "base.css",
"updated": "34627"
}
],
"html":[
{
"path": "home.htm",
"updated": "3223"
},
{
"path": "about",
"updated": "3987"
}
]
}
I want to search the "html" -> "home.htm" row. How to make use of filter method for it.
I got example like this for simple json structure, but in my case I have no idea.
{
countryid : "1"
name : "New York"
},
{
countryid : "2"
name : "Sydeny"
}
if let array = arrCountry.arrayObject as? [[String:String]],
foundItem = array.filter({ $0["name"] == "def"}).first {
print(foundItem)
}
let updated = json["html"].array?.filter { $0["path"].string == "home.htm" }.first?["updated"].string
print(updated)
// OR
for subJson in json["html"].arrayValue where subJson["path"].stringValue == "home.htm" {
let updated = subJson["updated"].stringValue
print(updated)
}

how to check if a particular json key has a value which is same for all the keys

For example, I have a json response of array type where the second key in each array has same value. The count of the array is dynamic but I need to check everytime, if that particular key value is same in all the arrays, i need to hide a label.
"loadable": [
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
}
}
]
Here i want to check if all the values for the key positionType == "XXX", then i need to hide a label. Please provide the answer in swift.
If you want to check each value is equal in array then you can make it like this.
let searchValue = "xxx"
if loadables.index(where: {$0.position?.positionType != searchValue}) != nil {
//positionType for all objects are not equal to searchValue
footer.labelTitle.isHidden = false
}
else {
//positionType for all objects are equal to searchValue
footer.labelTitle.isHidden = true
}
This is less of a json problem but more a question of "Do all elements in my dictionary have the same value", where the elements would be the key that you are looking for.
func checkValues(array: Array<Element>) -> Bool {
guard let myValue = array.firstObject()?.position?.positionType else {
return false
}
for element in array {
if element.position.positionType != myValue {
return false
}
}
return true
}

Ruby use find to loop through an array and return the matching value

I am trying to loop through an array using find to find and return a specific id.
This is my structure:
{
"employees": [
{
"emp_id": "1",
"tutorials": [
{
"id": "test1"
},
{
"id": "test2"
},
{
"id": "test3"
},
{
"id": "test4"
},
{
"id": "test5"
}
]
}
]
}
So basically I am trying to see if the above structure contains a tutorial id of 'test3' and return it.(i.e return 'test3' in this case)
I can get the desired result using a combination of map and find like this:
my_tutorial = employees.map { |employee|
employee.tutorials.find { |tutorial|
tutorial.id == 'test3'
}
}.first
my_tutorial
But I want to know if there is a better way using find . I tried the following but it returns the ruby object instead of the id.
employees.find { |employee|
employee.tutorials.find { |tutorial|
tutorial.id == 'test3'
}
}
Here is what i did to make it work using find. Not sure if it is any better:
my_id = employees.find { |employee|
employee.tutorials.find { |tutorial|
tutorial.id == 'test3'
}
}
my_id.tutorials.first.id
If you have to get only the first record do as follows:
employees[0].tutorials.detect {|r| r.id == 'test3' }

Resources