如何将父对象的id推送给子对象?

问题描述 投票:0回答:6

我有一个对象数组,其中有一个内部对象数组,我想将父对象的 id 推送到每个子对象。

   a = [
        {id: 'abc', stage: [{name: 'car' , value: '123'},{name: 'bus' , value: '345'},{name: 'truck' , value: '567'}],
        {id: 'def', stage: [{name: 'bike' , value: '890'},{name: 'cycle' , value: '123'},{name: 'car' , value: '456'}]}
    ]

expected output = [
    {name: 'car' , value: '123', id: 'abc'},{name: 'bus' , value: '345', 'abc'},{name: 'truck' , value: '567', 'abc'}, {name: 'bike' , value: '890', id: 'def'},{name: 'cycle' , value: '123',id: 'def',id: 'def'},{name: 'car' , value: '456', id: 'def'}
]

我能够获得唯一的舞台,但无法将 id 推送到每个对象。请帮忙

const getAllStages = [].concat(...map(a, el => el.stage));
console.log(getAllStages )
javascript angularjs lodash
6个回答
3
投票

再次使用

.map()
el.id
添加到
el.stage
的每个元素。

您可以在外部映射中使用

.flatMap()
将所有结果连接到一个数组中。

const a = [
    {id: 'abc', stage: [{name: 'car' , value: '123'},{name: 'bus' , value: '345'},{name: 'truck' , value: '567'}]},
    {id: 'def', stage: [{name: 'bike' , value: '890'},{name: 'cycle' , value: '123'},{name: 'car' , value: '456'}]}
]

result = a.flatMap(({
  id,
  stage
}) => stage.map(s => ({
  id: id,
  ...s
})));

console.log(result);


0
投票

如果您尝试合并

id

let remapped = a.map(e => ({ id: a.id, ...e }));

将每个条目转换为继承

a.id
值的对象,并添加对象中的其他内容。


0
投票

这是一个示例和结果:)

a.forEach(function(row) {
    row.stage.map(function (child) {child.id = row.id})
})

结果:

[{"id":"abc","stage":[{"name":"car","value":"123","id":"abc"},{"name":"bus","value":"345","id":"abc"},{"name":"truck","value":"567","id":"abc"}]},{"id":"def","stage":[{"name":"bike","value":"890","id":"def"},{"name":"cycle","value":"123","id":"def"},{"name":"car","value":"456","id":"def"}]}]

0
投票

不使用库也可以这样写

let a = [
    {
        id: 'abc', stage:
            [
                { name: 'car', value: '123' },
                { name: 'bus', value: '345' },
                { name: 'truck', value: '567' }
            ]
    },
    {
        id: 'def', stage:
            [
                { name: 'bike', value: '890' },
                { name: 'cycle', value: '123' },
                { name: 'car', value: '456' }
            ]
    }
]    
let output = [];
    for (const element of a) {
        for (const stageElement of element.stage) {
            let newElement = {
                name: stageElement.name,
                value: stageElement.value,
                id: element.id
            };
            output.push(newElement);
        }
    }

0
投票
const a = [
    {id: 'abc', stage: [{name: 'car' , value: '123'},{name: 'bus' , value: '345'},{name: 'truck' , value: '567'}]},
    {id: 'def', stage: [{name: 'bike' , value: '890'},{name: 'cycle' , value: '123'},{name: 'car' , value: '456'}]}
]

a.forEach(item => {
    const itemId = item.id;
    const stages = item.stage;

    stages.forEach(stage => {
        stage['id'] = itemId;
    })
});

console.log(a);

0
投票
import { Component, forwardRef, Input, Self, Optional, AfterViewInit } from '@angular/core';
import { ControlValueAccessor, FormControl, NgControl } from '@angular/forms';
import { of } from 'rxjs';
import { skipWhile, take } from 'rxjs/operators';
import {RequiredFormControl} from './required-form-control';

export abstract class AbstractFormFieldComponent implements ControlValueAccessor, AfterViewInit {
  _formControl = new FormControl(); 
  onChange = (value: any) => {};

 constructor(@Self() @Optional() public ngControl: NgControl) {
    if(this.ngControl) {
      this.ngControl.valueAccessor = this;
    }
  }

  ngAfterViewInit(): void {
    if (this.ngControl) {
      of(this.ngControl.control)
        .pipe(
          skipWhile(fc => !fc),
          take(1)
        )
        .subscribe(fc => {
          this.formControl = fc as FormControl;
        });
    }
  }

  get formControl() :FormControl|RequiredFormControl {
    return this._formControl;
  }
  set formControl(forControl:FormControl|RequiredFormControl)  {
    this._formControl = forControl;
  }

  registerOnChange(fn: (value: any) => void): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: (value: any) => void): void {}

  writeValue(value: any): void {
    if(this.formControl) this.formControl.setValue(value, { emitEvent: false });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.