How to create array from computed property array field in vue? - vue.js

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;
},
}

Related

push array instead o string in react native

I want to push my data in array but it shows me string in react native.
My code is like that:
const dataSource = responseJson.old_cases.reduce(function (sections, item) {
let section = sections.find(section => section.gender === item.gender);
if (!section) {
section = { gender: item.gender,data:[] };
sections.push(section);
}
section.data.push(item.name)
return sections;
}, []);
this.setState({dataSource: dataSource // Pass the dataSource that we've processed above});
Output of my code is
[
{"gender": "Male", "data": ["name_1", "name_2"]},
{"gender": "Female", "data": ["name_3", "name_4",'name_5']},
]
as you see names are string I need that kind of output
[
{"gender": "Male", "data": [{"name_1"}, {"name_2"}]},
{"gender": "Female", "data": [{"name_3"}, {"name_4"},{'name_5'}]},
]
We cannot apply string values inside the objects.
like.
[
{"gender": "Male", "data": [{"name_1"}, {"name_2"}]},
{"gender": "Female", "data": [{"name_3"}, {"name_4"},{'name_5'}]},
]
We have to apply key-value pairs like below:
section.data.push({ [item.name]: item.name })
Copy this and replace for your code, I think it's what you need. But I'm not sure.
const dataSource = responseJson.old_cases.reduce(function (sections, item) {
let section = sections.find(section => section.gender === item.gender);
if (!section) {
section = { gender: item.gender,data:[] };
sections.push(section);
}
section.data.push({[item.name]: item.name})
return sections;
}, []);
this.setState({dataSource: dataSource // Pass the dataSource that we've processed above});

v-select with groups from data via api

I get from my API this data:
[
{
"id":69,
"name":"Baupraxis",
"upper_id":7
},
{
"id":42,
"name":"bautipps.de",
"upper_id":4
},
{
"id":16,
"name":"Bellevue",
"upper_id":7
},
{
"id":18,
"name":"besser Bauen",
"upper_id":7
},
{
"id":2,
"name":"Besuch auf Betriebsgel\u00e4nde",
"upper_id":0
},
{
"id":7,
"name":"billiger Bauen",
"upper_id":0
}
]
I use it to fill a v-select like this:
<v-select
label="Anfrageart"
dense
:items="itemsRequestTypes"
item-text="name"
item-value="id"
v-model="requestType"
>
</v-select>
So here's what I'd like to solve, the upper_id is greater than zero if it is a subgroup of an item with the matching id. Means in upper_id is the id of the main group.
How can I group this in a v-select now?
I try to add a header in the object, but this didn't help.
You need to transform your data from the API before passing it to the template. Also, v-select supports nested option group using items where header type represents the group header which is non-selectable item.
const data = [
{
"id": 69,
"name": "Baupraxis",
"upper_id": 7
},
{
"id": 42,
"name": "bautipps.de",
"upper_id": 4
},
{
"id": 16,
"name": "Bellevue",
"upper_id": 7
},
{
"id": 18,
"name": "besser Bauen",
"upper_id": 7
},
{
"id": 2,
"name": "Besuch auf Betriebsgel\u00e4nde",
"upper_id": 0
},
{
"id": 7,
"name": "billiger Bauen",
"upper_id": 0
}
];
// Create an intermediate object to hold categories.
const groups = {};
// Create array for each group to main subgroup list.
data.forEach((x) => {
// create empty object if it doesn't exists.
groups[x.upper_id] = groups[x.upper_id] || { name: x.name, list: [] };
groups[x.upper_id].list.push(x);
});
// The flattened list of items that holds items as well as unique headers
const flattened = [];
// Iterate over all the unique categories and
// then flatten into a list that v-select needs.
Object.keys(groups).forEach((categoryId) => {
const catgory = groups[categoryId];
const categoryName = category.name;
// Create a group
flattened.push({ header: categoryName });
// Add all the items followed by category header
flattened.push(...category.list);
});

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

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 = ...

Update Amcharts4 chart dynamically using vue js

I'm using AmCharts4 with Vue.JS,
For the moment I've added default chart design when page loads, But when I trying to add dynamic values after page loads (Using Button click), It doesn't reflect on view.
Code:- gist
data: () => ({
dataForChart: {
data: [{
"name": "Anne",
"steps": 32
}, {
"name": "Rose",
"steps": 30
}, {
"name": "Jane",
"steps": 25
}]
}
}),
chart creation on mounted()
let chart = am4core.create("chart-div", am4charts.XYChart);
chart.data = this.dataForChart.data
when dynamically change values using button click those data doesnt reflect on chart.
method followed to change data set.
this.dataForChart.data = {
data: [{
"name": "Anne",
"steps": 54
}, {
"name": "Rose",
"steps": 44
}, {
"name": "Jane",
"steps": 33
}]
}
The reason for this is that although this.dataForChart.data is reactive (it's a vue instance property) the chart.data (amcharts chart property) is not.
You have to manually set chart.data array values whenever your this.dataForChart.data property changes. I've done this by setting a watcher on this.dataForChart.data like so:
watch: {
dataForChart: {
data(values) {
this.chart.data = values;
},
deep: true
}
}
Cheers!

Remove Object inside object by object property

I have an Object like this
{
"6":{
"id":2045,
"categories":[
{
"id":7,
"name":"Day Trips & Excursions Outside the City"
},
{
"id":2,
"name":"Day-Tour"
},
{
"id":8,
"name":"Food, Wine & Gastronomy"
}
],
},
"8":{
"id":2045,
"categories":[
{
"id":7,
"name":"Day Trips & Excursions Outside the City"
},
{
"id":2,
"name":"Day-Tour"
},
{
"id":8,
"name":"Food, Wine & Gastronomy"
}
],
},
},
Now I just want to delete every object if its property(id) matches from a user supplied id
[2045, 1234]
So in this case, I want to delete the object with id 2045 or 1234
I tried multiple solution but failed, I just want to update the object something
const categorytobedeleted = [2045, 12345];
for(const v of categorytobedeleted) {
const newobject = Object.values(oldobject).map(function(x) {
return x
}).indexOf(v);
Object.values(oldobject).slice(newobject, 1)
}
this.oldobject = this.newobject // maybe old object recreated with newly created object after deleting
You can use reduce for this purpose:
const newObject = Object.keys(oldobject).reduce((a, oldKey) => {
if (!categorytobedeleted.includes(oldobject[oldKey].id)) {
a[oldKey] = oldobject[oldKey]
}
return a
}, {})