Object inside of an array - kotlin

What I want to do is to add an object in array something like code below, but my Ide returns some errors, I don't understand what the problem is
val sachmelebi = [
{
name: 'ხინკალი',
link: 'https://www.google.com/search?q=%E1%83%AE%E1%83%98%E1%83%9C%E1%83%99%E1%83%90%E1%83%9A%E1%83%98&oq=%E1%83%AE%E1%83%98%E1%83%9C%E1%83%99%E1%83%90%E1%83%9A%E1%83%98&aqs=chrome..69i57j46j0l6.1143j0j9&sourceid=chrome&ie=UTF-8'
},
{
name: 'მწვადი',
link: 'https://www.google.com/search?q=%E1%83%9B%E1%83%AC%E1%83%95%E1%83%90%E1%83%93%E1%83%98&oq=%E1%83%9B%E1%83%AC%E1%83%95%E1%83%90%E1%83%93%E1%83%98&aqs=chrome.0.69i59j0l7.983j0j9&sourceid=chrome&ie=UTF-8'
},
{
name: 'მაკარონის წასახემსებელი',
link: 'https://gemrielia.ge/recipe/6594-xraSuna-makaronis-wasaxemsebeli-romelic-wuTebSi-mzaddeba/'
}

The code snippet provided in the question is not valid Kotlin syntax. I would suggest to create a data class for your entries and then store them in a list. Below you can find an example:
data class Entry( // use a better name than "Entry"
val name: String,
val link: String
)
val sachmelebi = listOf(
Entry(
"ხინკალი",
"https://www.google.com/search?q=%E1%83%AE%E1%83%98%E1%83%9C%E1%83%99%E1%83%90%E1%83%9A%E1%83%98&oq=%E1%83%AE%E1%83%98%E1%83%9C%E1%83%99%E1%83%90%E1%83%9A%E1%83%98&aqs=chrome..69i57j46j0l6.1143j0j9&sourceid=chrome&ie=UTF-8"
),
Entry(
"მწვადი",
"https://www.google.com/search?q=%E1%83%9B%E1%83%AC%E1%83%95%E1%83%90%E1%83%93%E1%83%98&oq=%E1%83%9B%E1%83%AC%E1%83%95%E1%83%90%E1%83%93%E1%83%98&aqs=chrome.0.69i59j0l7.983j0j9&sourceid=chrome&ie=UTF-8"
),
Entry(
"მაკარონის წასახემსებელი",
"https://gemrielia.ge/recipe/6594-xraSuna-makaronis-wasaxemsebeli-romelic-wuTebSi-mzaddeba/"
)
)

Related

All values getting upadted when tried to update single element in an associative array in Vue 3

I have the below code in Vue3:
data: function() {
return {
testData:[],
}
},
mounted() {
var testObj = {
name: 'aniket',
lastname: 'mahadik'
}
for (let index = 0; index < 3; index++) {
this.testData.push(testObj);
}
},
methods: {
updateLastName: function(key) {
this.testData[key].lastname = 'kirve';
}
}
When I call updateLastName(1) to update the lastname of only the second element, it's updating the lastname of all the elements.
I tried several ways but found no desired result.
Can someone point out to me what is going wrong here?
It is because you are pushing the reference to the same object in the array so when you update any item in the array you are instead updating every item since it reference the same object.
Either push by cloning the object :
testData.value.push({...testObj})
Or put the definition in the push
testData.value.push({ name: 'aniket', lastname: 'mahadik' })
Is JavaScript a pass-by-reference or pass-by-value language?

Generic Class as a Default Response

I was trying to create a default response class for my API, thus avoiding returning the specific object.
But the problem is, in Java I can do it, but in Kotlin I can't :( .
Here is my Controller:
#GetMapping("/search/{title}")
fun returnMovie(#PathVariable("title") title: String,
#RequestHeader("include_adult") includeAdult: Boolean,
#RequestHeader("page") page: String,
#RequestHeader("language") language: String): Response {
//WHAT I HAVE TO DO?
return movieClient.getMovie(title, includeAdult, page, language, apiKey)
}
Previously it was directly returning the Movie object.
#Document
data class Movie(
#Id
val id: String? = ObjectId.get().toString(),
val genre_ids: List<Int>?,
val original_language: String?,
val original_title: String?,
val overview: String?,
val release_date: Date?
)
But now, I would like to return this object with a generic field so I can assign other objects.
data class Response(
val page: Int?,
val content: MutableList<T>?,
val total_pages: Int?,
val total_results: Int?
)
UPDATE
Here is the return
{
"page": 1,
"results": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
"total_pages": 1,
"total_results": 18
}
How can I bind my Movie object to the content parameter generic ?
UPDATE
I get a response from this API via FeignClient and it was mapping to the Movie object.
#RequestMapping(method = [RequestMethod.GET], value = ["search/movie"], consumes = ["application/json"])
fun getMovie(#RequestParam("query") query: String,
#RequestParam("include_adult") includeAdult: Boolean,
#RequestParam("page") page: String,
#RequestParam("language") language: String,
#RequestParam("api_key") apiKey: String): MutableList<Movie>
This line is your issue:
val content: MutableList<T>?
The T doesn't make sense there, that is a typed parameter that is for generic functions and etc. If you want a list of any (hint) type of object, you need your list to be
val content: MutableList<Any>?

Is there buttons / funtion calls & tables handled in MetaWidget - js

I am wondering if some can help me out to get the buttons , function calls and tables displayed using metawidget jsonSchema.
Unfortunately, I can see it just as a form render-er for our applications, is it something which we need to define externally? also if we could navigate from one form to another somehow
<script type="text/javascript">
var mw = new metawidget.Metawidget( document.getElementById( 'metawidget' ), {
inspector: new metawidget.inspector.CompositeInspector( [ new metawidget.inspector.PropertyTypeInspector(),
function( toInspect, type, names ) {
return {
properties:
name: {
required: true
},
notes: {
type: "string",
large: true
},
employer: {
type: "string",
section: "Work"
},
department: {
type: "string"
}
}
};
} ] ),
layout: new metawidget.jqueryui.layout.TabLayoutDecorator(
new metawidget.layout.TableLayout( { numberOfColumns: 2 } ))
} );
mw.toInspect = person;
mw.buildWidgets();
Above schema holds only properties to render the fields, but where to specify the functionalities?
Assuming the object you are inspecting has functions, in JSON schema you can specify the property with a type of function. Metawidget will render these as buttons (and will wire up their click handler).
Note that PropertyTypeInspector will find functions automatically, so you could also consider combining your JsonSchemaInspector with a PropertyTypeInspector using CompositeInspector.
Often your data object (e.g. var person = { firstname: 'Homer', surname: 'Simpson' } ) and your actions object (e.g. var personActions = { save: function() {...}, delete: function() {...}} ) are separate. In those cases you can position two Metawidgets as siblings (or nested within each other), each pointing at different objects.
Finally for tables use a type of array. In JSON schema, you nest an items object that further nests a properties object, to describe the properties of the array items. See this example http://blog.kennardconsulting.com/2016/04/metawidget-and-angular-arrays.html (it's for Angular, but the JSON Schema is the same)

Find object by match property in nested array

I'm not seeing a way to find objects when my condition would involve a nested array.
var modules = [{
name: 'Module1',
submodules: [{
name: 'Submodule1',
id: 1
}, {
name: 'Submodule2',
id: 2
}
]
}, {
name: 'Module2',
submodules: [{
name: 'Submodule1',
id: 3
}, {
name: 'Submodule2',
id: 4
}
]
}
];
This won't work because submodules is an array, not an object. Is there any shorthand that would make this work? I'm trying to avoid iterating the array manually.
_.where(modules, {submodules:{id:3}});
Lodash allows you to filter in nested data (including arrays) like this:
_.filter(modules, { submodules: [ { id: 2 } ]});
Here's what I came up with:
_.find(modules, _.flow(
_.property('submodules'),
_.partialRight(_.some, { id: 2 })
));
// → { name: 'Module1', ... }
Using flow(), you can construct a callback function that does what you need. When call, the data flows through each function. The first thing you want is the submodules property, and you can get that using the property() function.
The the submodules array is then fed into some(), which returns true if it contains the submodule you're after, in this case, ID 2.
Replace find() with filter() if you're looking for multiple modules, and not just the first one found.
I think your best chance is using a function, for obtaining the module.
_.select(modules, function (module) {
return _.any(module.submodules, function (submodule) {
return _.where(submodule, {id:3});
});
});
try this for getting the submodule
.where(.pluck(modules, "submodules"), {submodules:{id:3}});
I looked into this and I think the best option is to use Deepdash. It's a collection of methods to do deeply filter, find etc.
Sure it would be possible with lodash alone but with Deepdash it's easier.
I tried to convert the previous answer to the latest Lodash version but that was not working. Every method was deprecated in v4 of Lodash. Possible replacements: select = map or filter, any = some, where = filter)
findDeep returns an object with some information to the found item (just some values, see the docs for more details):
value is the object found
key that's the index in the nested array
parent the parent of the value
So the code for the findDeep looks like:
const modules = [{
name: 'Module1',
submodules: [{
name: 'Submodule1',
id: 1
}, {
name: 'Submodule2',
id: 2
}]
}, {
name: 'Module2',
submodules: [{
name: 'Submodule1',
id: 3
}, {
name: 'Submodule2',
id: 4
}]
}];
const getModule = (modules, id) =>
_.findDeep(modules, module => module.id === id, {
childrenPath: "submodules"
});
const resultEl = document.getElementById("result");
const foundModule = getModule(modules, 3).value;
resultEl.innerText = JSON.stringify(foundModule, null, 2);
<script src="https://cdn.jsdelivr.net/npm/deepdash/browser/deepdash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script>
<script>
deepdash(_);
</script>
<h2>Example to get module with id = 3</h2>
<pre id="result"></pre>

Sencha Touch 2: Set a store from an array

I am trying to create a store, which I can then create a list from. The problem is when I create the store, it is empty.
This is my model:
Ext.define('MyApp.model.TrackingCode', {
extend: 'Ext.data.Model',
config: {
fields: [
'id',
'code',
'count'
]
}
});
And here's the code where I am trying to populate the store:
var tc = [{id: 1, code: 'abc', count: 5}];
var store = Ext.create('Ext.data.Store', {
model: 'MyApp.model.TrackingCode',
data: tc
});
The summary for console.log(store); shows this:
Class {_proxy: Class, data: Class, _totalCount: null, eventDispatcher: Class, initialConfig: Object…}
Which shows the total count value as null, so I don't think my store is getting populated.
Any insight would be appreciated.
_totalCount will be null when you load data by inline. _totalCount will have some value when you load data from server. In short _totalCount refers to the total set of data in server. So dont worry about it.
Try this
console.log(store.getCount());
This will return the exact count of data loaded in the store. If it returns some value, then your code is good.
You have to put the "data" property inside Store's config object like this:
var tc = [{id: 1, code: 'abc', count: 5}];
var store = Ext.create('Ext.data.Store', {
model: 'MyApp.model.TrackingCode',
config : {
data: tc
}
});