改变Vue3 TypeScript中的对象

huangapple go评论67阅读模式
英文:

Change Object in vue3 typeScript

问题

[
  {
    "name": "input1",
    "value": "1,827"
  },
  {
    "name": "input2",
    "value": "310"
  },
  {
    "name": "loads1",
    "value": "440"
  },
  {
    "name": "loads2",
    "value": "220"
  },
  {
    "name": "loads3",
    "value": "46"
  },
  {
    "name": "products1",
    "value": "15,000"
  },
  {
    "name": "products3",
    "value": "4.8"
  }
]
英文:

My original object is as follows (Object name is runOverview). I want to remove bold fields version, inputs, loads, products and project_name.

> {
"version": "1",
"inputs": [
{
"name": "input1",
"value": "1,827"
},
{
"name": "input2",
"value": "310"
}
],
"loads": [
{
"name": "loads1",
"value": "440"
},
{
"name": "loads2",
"value": "220"
},
{
"name": "loads3",
"value": "46"
}
],
"products": [
{
"name": "products1",
"value": "15,000"
},
{
"name": "products3",
"value": "4.8"
}
],
"project_name": "Test"
}

I want to change runOverview object as bellow (expected output).

>
{{
"name": "input1",
"value": "1,827"
},
{
"name": "input2",
"value": "310"
},
{
"name": "loads1",
"value": "440"
},
{
"name": "loads2",
"value": "220"
},
{
"name": "loads3",
"value": "46"
},
{
"name": "products1",
"value": "15,000"
},
{
"name": "products3",
"value": "4.8"
}}

I've tried as follows

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const overviewDetails = computed(() =&gt; {
  const overviewArr = [];
    for ( var newdata in runOverview.value) {
      overviewArr.push(newdata)
    }
  return overviewArr
});

<!-- end snippet -->

Please help me to create new object array with only necessary properties

答案1

得分: 1

这非常强大,它将合并任何值为数组的属性。我相信这就是您想要的:

// </script><script type="module">
import { ref, computed } from "https://unpkg.com/vue@3.2.47/dist/vue.runtime.esm-browser.js";

const runOverview = ref({ "version": "1", "inputs": [ { "name": "input1", "value": "1,827" }, { "name": "input2", "value": "310" } ], "loads": [ { "name": "loads1", "value": "440" }, { "name": "loads2", "value": "220" }, { "name": "loads3", "value": "46" } ], "products": [ { "name": "products1", "value": "15,000" }, { "name": "products3", "value": "4.8" } ], "project_name": "Test" });

const overviewDetails = computed(() => {
  return Object.values(runOverview.value).reduce((r, item) => {
    if (Array.isArray(item)) r.push(...item)

    return r
  }, [])
})

console.log(overviewDetails.value)
英文:

this is very powerful it will merge any property whose value is an array. I believe this is what you want:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// &lt;/script&gt;&lt;script type=&quot;module&quot;&gt;
import { ref, computed } from &quot;https://unpkg.com/vue@3.2.47/dist/vue.runtime.esm-browser.js&quot;

const runOverview = ref({ &quot;version&quot;: &quot;1&quot;, &quot;inputs&quot;: [ { &quot;name&quot;: &quot;input1&quot;, &quot;value&quot;: &quot;1,827&quot; }, { &quot;name&quot;: &quot;input2&quot;, &quot;value&quot;: &quot;310&quot; } ], &quot;loads&quot;: [ { &quot;name&quot;: &quot;loads1&quot;, &quot;value&quot;: &quot;440&quot; }, { &quot;name&quot;: &quot;loads2&quot;, &quot;value&quot;: &quot;220&quot; }, { &quot;name&quot;: &quot;loads3&quot;, &quot;value&quot;: &quot;46&quot; } ], &quot;products&quot;: [ { &quot;name&quot;: &quot;products1&quot;, &quot;value&quot;: &quot;15,000&quot; }, { &quot;name&quot;: &quot;products3&quot;, &quot;value&quot;: &quot;4.8&quot; } ], &quot;project_name&quot;: &quot;Test&quot; })

const overviewDetails = computed(() =&gt; {
  return Object.values(runOverview.value).reduce((r, item) =&gt; {
    if (Array.isArray(item)) r.push(...item)

    return r
  }, [])
})

console.log(overviewDetails.value)

<!-- end snippet -->

答案2

得分: 0

const arr = []
Object.values(runOverview).forEach(item => {
    if(Array.isArray(item)) {
        arr.push(...item)
    }
})
console.log(arr)
英文:
const arr = []
Object.values(runOverview).forEach(item =&gt; {
        if(Array.isArray(item)) {
            arr.push(...item)
        }
    })
console.log(arr)

答案3

得分: 0

你可以使用 reducemap 方法。尝试这样做:

<script type="module">
import { ref, computed } from "https://unpkg.com/vue@3.2.47/dist/vue.runtime.esm-browser.js";

const runOverview = ref({ version: "1", inputs: [{ name: "input1", value: "1,827" }, { name: "input2", value: "310" }], loads: [{ name: "loads1", value: "440" }, { name: "loads2", value: "220" }, { name: "loads3", value: "46" }], products: [{ name: "products1", value: "15,000" }, { name: "products3", value: "4.8" }], project_name: "Test" });

const overviewDetails = computed(() =>
  ["inputs", "loads", "products"].reduce(
    (acc, key) =>
      acc.concat(
        runOverview.value[key].map((item) => ({
          name: item.name,
          value: item.value,
        }))
      ),
    []
  )
);
console.log(overviewDetails.value)
</script>
英文:

You can use reduce and map methods. Try this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script type=&quot;module&quot;&gt;
import { ref, computed } from &quot;https://unpkg.com/vue@3.2.47/dist/vue.runtime.esm-browser.js&quot;

const runOverview = ref({version:&quot;1&quot;,inputs:[{name:&quot;input1&quot;,value:&quot;1,827&quot;},{name:&quot;input2&quot;,value:&quot;310&quot;},],loads:[{name:&quot;loads1&quot;,value:&quot;440&quot;},{name:&quot;loads2&quot;,value:&quot;220&quot;},{name:&quot;loads3&quot;,value:&quot;46&quot;},],products:[{name:&quot;products1&quot;,value:&quot;15,000&quot;},{name:&quot;products3&quot;,value:&quot;4.8&quot;},],project_name:&quot;Test&quot;});

 const overviewDetails = computed(() =&gt;
  [&quot;inputs&quot;, &quot;loads&quot;, &quot;products&quot;].reduce(
    (acc, key) =&gt;
      acc.concat(
        runOverview.value[key].map((item) =&gt; ({
          name: item.name,
          value: item.value,
        }))
      ),
    []
  )
);
console.log(overviewDetails.value)

&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 12:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定