Vue.js: How can I update an array of objects? - vue.js

I am trying to update an array of objects in Vue.js. When trying to update the values of my location I am struggling to update the objects within an array.
When I log the objects out, I get this:
console.log(this.location) // {…}
console.log(this.location.additionalProperties); // [{…}, __ob__: Observer]
console.log(this.location.additionalProperties.integrations); // undefined
My additionalProperties object looks like this:
"additionalProperties": [
{
"integrations": [
{
"foo": {
"locationId": 123,
"example": "bar",
...
}
}
]
}
],
I am passing in my location as a props like this:
location: {
type: Object,
required: true,
default: () => ({}),
},
I know I am getting the location passed in correctly. I believe it is a syntax issue I am struggling with. I am trying to set my foo object to be something like this:
this.location.additionalProperties.integrations.foo = {
locationId: 456,
example: "testing",
somethingElse: "anotherValue"
}
Using the above, I'll get a version of cannot set foo of undefined. How can I update the object within the additionalProperties array?
Thank you for any suggestions!

additionalProperties is an array
"additionalProperties": [
{
"integrations": [
{
"foo": {
"locationId": 123,
"example": "bar",
...
}
}
]
}
],
this.location.additionalProperties[0].integrations.foo = ...

Related

restrict collection.find() should only return non-empty object but not empty objects inside array

const response = await simpleSurveyResponsesModel
.find({
"metaKey.projectId": 7,
})
.select([
"answers.SEC.text",
"answers.S4.text",
"answers.S3_a.text",
"answers.Urban_City.text"
])
.lean();
Where answers feild is array of objects containing both empty ad non-empty objects.
collection.find() returns the selected objects but also empty objects as well, inside answers array which i dont need.
My Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{},
{},
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}
Expected Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}

Ramda - how to add new property to objects in array

I am trying to add a new property to each object in the array. My test array:
const array = [
{
"name": 'Test'
},
{
"name": 'Test2'
}
]
I tried to use R.assoc('selected', false, array); but it didn't work as expected.
As a result, I would like to get:
const array = [
{
"name": 'Test',
"selected": false
},
{
"name": 'Test2',
"selected: false
}
]
Any help will be appreciated
R.assoc works by making a shallow clone of an object, and setting/updating a property. In this case, you need to handle an array of objects. Use R.map to iterate the array, and create a new array with transformed cloned objects by applying R.assoc:
const fn = R.map(R.assoc('selected', false));
const array = [{"name":"Test"},{"name":"Test2"}];
const result = fn(array);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

How to create array from computed property array field in vue?

I have a computed property that pulls some data out of my vuex store like so:
computed: {...mapGetters(["allCategories"])},
Each category in this.allCategories looks like so:
{ "id": "123", "name": "Foo" }
I want to pull out every name field from this.allCategories before the component is mounted in put each name into an reactive data property called categoryNames.
How can I achieve this?
What I have tried so far is below:
beforeMount() {
for (let i = 0; i < this.allCategories.content.length; i++) {
var name = this.allCategories.content[i].name
this.categoryNames.push(name);
}
},
Which gives the following error:
Error in beforeMount hook: "TypeError: Cannot read property 'length' of undefined"
this.allCategories looks like so:
{
"content": [
{
"id": "efb038df-4bc9-4e31-a37a-e805c9d7294e",
"parentCategoryId": "8ffc214f-fff1-4433-aac9-34d13b4e06c5",
"name": "Foo"
},
{
"id": "5905d437-db2e-4f91-8172-c515577b86e9",
"parentCategoryId": "5905d437-db2e-4f91-8172-c515577b86e9",
"name": "Bar"
},
{
"id": "8ffc214f-fff1-4433-aac9-34d13b4e06c5",
"parentCategoryId": "8ffc214f-fff1-4433-aac9-34d13b4e06c5",
"name": "Baz"
}
],
"number": 0,
"size": 100,
"total": 3
}
You could use the created hook to call a vuex action that calls a vuex mutation that grabs a state, do your parsing and store the parsed data in a new array in state, then use a getter to grab the parsed array from state.
created() {
this.$store.dispatch('someAction');
},
computed: {
...mapGetters({
parsedArray: 'getParsedArray',
})
}
export const actions = {
someAction({ commit }) {
commit('SOME_MUTATION');
},
}
export const mutations = {
SOME_MUTATION(state) {
let data = state.originalData;
let parsedArray = [];
// Do parsing work here
state.parsedArray = parsedArray
},
}
export const getters = {
getParsedArray: state => {
return state.parsedArray;
},
}

Karate; Object type not being throws TypeError as "not an Object"

With my testing I am looking to do some evaluating on json objects within an array. The example looks like this:
"sections": [
{
"internal_id": 10635,
"uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
},
{
"internal_id": 10636,
"uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
},
{
"internal_id": 10637,
"uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
},
{
"internal_id": 10638,
"uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
}
]
While doing some debugging I did this:
* print typeof(sections[0]) => this comes back saying object
I have tried to evaluate individual objects doing this:
* eval Object.keys(sections[0]).length; => this comes back with TypeError: <object_at_index_0> is not an Object in <eval>
I don't have a lot of experience with JS, but could someone help me out with this?
You can write an expected schema for a single JSON object and then use match each to validate it for all the data in the JSON Array.
* def sectionArray =
"""
{
"sections": [
{
"internal_id": 10635,
"uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
},
{
"internal_id": 10636,
"uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
},
{
"internal_id": 10637,
"uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
},
{
"internal_id": 10638,
"uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
}
]
}
"""
* match each sectionArray.sections == {"internal_id":"#present","uuid":"#present"}
You can also validate the size of the array by,
* match sectionArray.sections == "#[4]"
Refer Fuzzy Matching

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