Find object in array of objects with same id - dojo

I have created classes
dojo.declare("UNIT",null,{
_id:'',
constructor:function(i){
this._id=i;
});
and
dojo.declare("ELEMENT", null, {
_id:'',
_unit_id:'',
constructor:function(u,i){
this._unit_id=u;
this._id=i;
});
I have array of Units and I want find one which have id like my element._unit_id. Hot to do this with Dojo ? I was looking in documentation examples but there is dojo.filter by I cannot pass argument . Can anybody help ?

You can use dojo.filter.E.g:
var units = [{
id: 1,
name: "aaaa"
},
{
id: 2,
name: "bbbb"
},
{
id: "2",
name: "cccc"
},
{
id: "3",
name: "dddd"
}];
var currentElementId = 2;
var filteredArr = dojo.filter(units, function(item) {
return item.id==currentElementId;
});
// do something with filtered array
}
Test page for you

Related

Filter array of objects based on another array in data

I have an array of 20 objects which I am getting from a database, each of them has an unique id. I also have an array of data with 2 ids. I want want to filter out only those 2 objects from the array of 20.
computed: {
newHeros(){
return this.getAllHeros.filter(newHero => {
console.log(newHero.id);
return newHero.id === this.heroForTab
})
}
},
return {
heroForTab: ['76NQjrYTdfbWN8xZOAvI', 'uDsm0BValBa31guJs10h']
};
User Array.filter to return what you need
var heroForTab = ['76NQjrYTdfbWN8xZOAvI', 'uDsm0BValBa31guJs10h'];
var arr = [{
id: '76NQjrYTdfbWN8xZOAvI',
name: 'aaa'
},
{
id: '1111',
name: 'bbb'
},
{
id: '2222',
name: 'ccc'
},
{
id: 'uDsm0BValBa31guJs10h',
name: 'ddd'
}
]
var result = arr.filter(item => {
return heroForTab.includes(item.id)
})
console.log(result)

Vuejs How to assign an array to another array in such a way that the two arrays are not reactive to changes

I faced with this challenge to assign array to another array such that the arrays won't be reactive on changes from one of the array.
So to achieve this I made a local copy of the same array and assigned one to another but I would love to do stuffs dynamically where I am having the reactive issues.
Below is what am trying to achieve
const ComponentA = {
data: function() {
return {
categories:[
{
name: 'Category 1',
series: [
{
name: 'Series 1',
value: 5
},
{
name: 'Series 2',
value: 5
}
]
},
{
name: 'Category 2',
series: [
{
name: 'Series 1',
value: 50
},
{
name: 'Series 2',
value: 56
}
]
}
],
tempCategories:[
{
name: 'Category 1',
series: [
{
name: 'Series 1',
value: 5
},
{
name: 'Series 2',
value: 5
}
]
},
{
name: 'Category 2',
series: [
{
name: 'Series 1',
value: 50
},
{
name: 'Series 2',
value: 56
}
]
}
]
}
},
methods: {
resetCategory() {
this.tempCategories.forEach((category,i)=>{
category.series.forEach((series,j)=>{
this.categories[i].series[j] = series;
});
});
}
}
}
after computations on categories i can reset the categories array with tempCategories by calling
this.resetCategory() and it works as expected.
Meanwhile I want to be able to do this assignment dynamically whereby when i have copy of the category array I can assign the array to both categories and tempCategories arrays, respectively and after computations with categories array i can reset the categories array with tempCategories array. So I did the following.
initialize(category) {
this.tempCategories = category;
this.categories = category;
}
I realize changes in this.categories reflects on this.tempCategories; I've even used the following options
initialize(category) {
this.tempCategories = category.slice();
//or this.tempCategories = Json.parse(Json.stringify(category));
this.categories = category.slice();
//or this.categories = Json.parse(Json.stringify(category));
}
yet the two arrays are still reactive. Please I need help on how to solve this problem.
Thanks guys, it works now both method
initialize(categories){
//let copy = _.cloneDeep(categories);
let copy = JSON.parse(JSON.stringify(categories));
this.tempCategories = copy;
this.categories = copy;
},
resetCategories(){
//this.categories = _.cloneDeep(this.tempCategories);
this.categories = JSON.parse(JSON.stringify(this.tempCategories));
},
There's no such Json. but It may be JSON.
initialize(category) {
this.tempCategories = JSON.parse(JSON.stringify(category));
this.categories = JSON.parse(JSON.stringify(category));
}
And you can't prevent fake copy of by
this.tempCategories = category;
this.categories = category;
May be those three Arrays are exactly same even one of them is updated.

Using Custom Sort with Track Scores set to True is still showing score as null

So I'm setting a default query in my React Native app. Essentially I'm trying to set a sortOrder based on the elementOrder values. My partner used this same piece of code in his web app and it works for him. It doesn't seem to work on my end. The score exists if I remove the custom sort, which is normal due to what I've read in the docs. When I'm using a custom sort, then I should add track_scores: true. My score is still coming up as null.
I am not sure how to debug this situation. Can someone point me in the right direction? Thanks! Here's my code and let me know if you need to see anything. Unfortunately I don't have access to Kibana. I'm just console logging the list item and it's properties.
const defaultQueryConfig = {
track_scores: true,
sort: {
_script: {
type: 'number',
script: {
lang: 'painless',
source: `
int sortOrder = 0;
if (doc['elementOrder'].value == 1) {sortOrder = 3}
else if (doc['elementOrder'].value == 3) {sortOrder = 2}
else if (doc['elementOrder'].value == 2) {sortOrder = 1}
sortOrder;
`,
},
order: 'desc',
},
},
query: {
function_score: {
query: {
match_all: {},
},
functions: [
{
filter: {
match: {
categoryType: 'earth',
},
},
weight: 100,
},
{
filter: {
match: {
categoryType: 'water',
},
},
weight: 90,
},
{
filter: {
match: {
categoryType: 'fire',
},
},
weight: 80,
},
{
filter: {
match: {
thingExists: false,
},
},
weight: 2,
},
],
score_mode: 'multiply',
},
},
};

Creating sorted tree in DOJO 1.6?

I new to learn dojo and trying to learn by it using samples code.
Using dojo 1.6
With help of sample codes , I created a tree
now i want to apply sorting on root and also on child.
With the help of this sample code , i changed the code
Output is not sorted n but the root folder has changed their position and child is deleted.
Plz help me to resolve this.
My code :
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.tree.ForestStoreModel");
dojo.require("dijit.Tree");
var data = [ { id: 1, name: "answerTypeLabel", type:'scenario', children:[{_reference: 2}]},
{ id: 2, name: "acceptRequestLabel", type:'paragraph', data: "acceptRequestLabel"},
{ id: 3, name: "rejectRequestLabel", type:'scenario', children:[{_reference: 5},{_reference: 6}]},
{ id: 4, name: "MoreInformationLabel", type:'scenario', children:[{_reference: 7},{_reference: 8}]},
{ id: 5, name: "rejectRequestStatusLabel", type:'paragraph', data: "rejectRequestStatusLabel"},
{ id: 6, name: "rejectRequestNotCoveredLabel", type:'paragraph', data: "rejectRequestNotCoveredLabel" },
{ id: 7, name: "MoreInformationDocumentLabel", type:'paragraph', data: "MoreInformationDocumentLabel"},
{ id: 8, name: "MoreInformationDataLabel", type:'paragraph', data: "MoreInformationDataLabel"}
];
dojo.addOnLoad(function() {
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'id',
label: 'name',
items: data
}
});
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'id',
items: [],
label: 'name'
}
}) // blank itemsstore
})
var tree = new dijit.Tree({
model: model,
updateItems: function(items) {
var self = this;
console.log('pre', this.model.root.children);
dojo.forEach(items, function(newItem) {
console.log('add', newItem);
try {
self.model.store.newItem({
id: sortableStore.getValue(newItem, 'id'),
name: sortableStore.getValue(newItem, 'name'),
type: sortableStore.getValue(newItem, 'type'),
data: sortableStore.getValue(newItem, 'data'),
});
} catch (e) {
console.log(e);
}
});
console.log('post', this.model.root.children);
console.log("children: ", this.rootNode.getChildren());
},
});
tree.placeAt(dojo.body());
sortableStore.fetch({
query: {
type:'scenario'
},
sort: [{
attribute: "name"}],
onComplete: function(items) {
console.log(items, 'sorted');
tree.updateItems(items);
}
})
});
Output :
The 'Names' origins from you setting 'rootLabel'.
Btw, fiddles have revisions and is simply a paste-bin like feature :)
You need to use the tree model pasteItem to insert referenced items (the 'children' property of each 'newItem').
Otherwise, there's another approach, if you get rid of the '_reference' structure of your data. See: http://jsfiddle.net/GHFdA/1/

ExtJS4 - Load Store From Another Grid

I am trying to load a json store when I click on a particular row in another grid. Can anyone see what I am doing wrong here? In the ext-all.js the error comes back as data is undefined (from debugger).
Ext.define('Documents', {
extend: 'Ext.data.Model',
fields: [
{ name: 'index', type: 'int' },
{ name: 'path', type: 'string' }
]
});
var documents = new Ext.data.JsonStore({
model: 'Documents',
root: 'groupdocuments',
autoLoad: false
});
// in the Ext.grid.Panel
listeners: {
itemclick: function () {
var itemgroupid = rec.get('groupid');
Ext.Ajax.request({
url: '/GetDocuments',
params: { groupId: itemgroupid },
success: function (result) {
var jsondata = Ext.decode(result.responseText);
documents.loadData(jsondata);
}
});
}
}
// the sample json returned from url
// { "groupdocuments": [{ "index": 1, "path": "1.doc" }, { "index": 2, "path": "2.doc" }, { "index": 3, "path": "3.doc" }] }
it looks like you need to escape the path data. should be { path: "C:\\something\\" }
Also why not use the grid Grouping feature?
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.feature.Grouping
In looking further it looks like the loaddata function is expecting an array. Not a json object with a rootdata object like you are giving it. change the listener to the following:
var jsondata = Ext.decode(result.responseText);
documents.loadData(jsondata.groupdocuments);
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store-method-loadData
alternatively you should be able to use loadRawData with the full json object.
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store-method-loadRawData