добавить значение в javascrpt - PullRequest
0 голосов
/ 06 августа 2020

У меня есть ответ json, и я попытался получить имя пользователя и типы для пользователя. Который сейчас работает нормально. Я хочу добавить географическое имя вместе с именем пользователя как user1 <Geo1> user2. Я пробовал как

const response = [
        {
          UserName: "User1",
          Type: "type3",
           Geo: "Geo1",
          month: "May",
          Week1: 2,
          Week2: 1
        },
        {
          UserName: "User2",
          Type: "type2",
          Geo: "Geo2",
          month: "July",
          Week1: 1
        },
        {
          UserName: "User1",
          Type: "type1",
          Geo: "Geo1",
          month: "Jun",
          Week2: 1,
          Week1: 1
        },
        {
          UserName: "User1",
          Type: "type2",
          Geo: "Geo1",
          month: "July",
          Week3: 1
        },
        {
          UserName: "User2",
          Type: "type3",
          Geo: "Geo2",
          month: "May",
          Week1: 2,
          Week2: 1
        },
        {
          UserName: "User2",
          Type: "type1",
          Geo: "Geo2",
          month: "Jun",
          Week2: 1,
          Week1: 1
        },
        {
          UserName: "User2",
          Type: "type3",
          Geo: "Geo3",
          month: "May",
          Week1: 2,
          Week2: 1
        },
      ];
const users = response.reduce((u, x) => {
        if (!(x.UserName in u)) u[x.UserName] = {};
        //if (!(x.UserName in u)) u[x.UserName]+u[x.Geo] = {}; my trial
        
        const user = u[x.UserName];
         //const Geouser = u[x.Geo];
         //user[x.Geo] ;
        if (!(x.Type in user)) user[x.Type] = {};
        const { UserName, Geo,Type, month, ...weeks } = x;
        user[x.Type][x.month.substring(0, 3)] = weeks;
        return u;
      }, {});
      console.log(users);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Я поймал Uncaught SyntaxError: Invalid left-hand side in assignment Ошибка

Ответы [ 4 ]

1 голос
/ 07 августа 2020

Добавить Geo к UserName.

const response = [
    {
        UserName: "User1",
        Type: "type3",
        Geo: "Geo1",
        month: "May",
        Week1: 2,
        Week2: 1
    },
    {
        UserName: "User2",
        Type: "type2",
        Geo: "Geo2",
        month: "July",
        Week1: 1
    },
    {
        UserName: "User1",
        Type: "type1",
        Geo: "Geo1",
        month: "Jun",
        Week2: 1,
        Week1: 1
    },
    {
        UserName: "User1",
        Type: "type2",
        Geo: "Geo1",
        month: "July",
        Week3: 1
    },
    {
        UserName: "User2",
        Type: "type3",
        Geo: "Geo2",
        month: "May",
        Week1: 2,
        Week2: 1
    },
    {
        UserName: "User2",
        Type: "type1",
        Geo: "Geo2",
        month: "Jun",
        Week2: 1,
        Week1: 1
    },
    {
        UserName: "User2",
        Type: "type3",
        Geo: "Geo3",
        month: "May",
        Week1: 2,
        Week2: 1
    },
];
const users = response.reduce((u, x) => {
    const key = `${x.UserName} <${x.Geo}>`;
    if (!(key in u))
        u[key] = {};
    const user = u[key];
    if (!(x.Type in user))
        user[x.Type] = {};
    const { UserName, Geo,Type, month, ...weeks } = x;
    user[x.Type][x.month.substring(0, 3)] = weeks;
    return u;
}, {});
console.log(users);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1 голос
/ 06 августа 2020

Мне кажется, ваш пример слишком сложен. Если вы хотите добавить новый ключ в свой массив объектов, почему бы не использовать map()?

А как насчет этого?

response.map(x => ({ ...x, nameWithGeo: `${x.UserName} ${x.Geo}` }))
0 голосов
/ 06 августа 2020

const response = [
        {
          UserName: "User1",
          Type: "type3",
           Geo: "Geo1",
          month: "May",
          Week1: 2,
          Week2: 1
        },
        {
          UserName: "User2",
          Type: "type2",
          Geo: "Geo2",
          month: "July",
          Week1: 1
        },
        {
          UserName: "User1",
          Type: "type1",
          Geo: "Geo1",
          month: "Jun",
          Week2: 1,
          Week1: 1
        },
        {
          UserName: "User1",
          Type: "type2",
          Geo: "Geo1",
          month: "July",
          Week3: 1
        },
        {
          UserName: "User2",
          Type: "type3",
          Geo: "Geo2",
          month: "May",
          Week1: 2,
          Week2: 1
        },
        {
          UserName: "User2",
          Type: "type1",
          Geo: "Geo2",
          month: "Jun",
          Week2: 1,
          Week1: 1
        },
        {
          UserName: "User2",
          Type: "type3",
          Geo: "Geo3",
          month: "May",
          Week1: 2,
          Week2: 1
        },
      ];
      
const users = {};
for(const user of response) {
  if (!users[user.UserName]) {
    users[user.UserName] = {};
  }
  if(!users[user.UserName][user.Geo]) {
    users[user.UserName][user.Geo] = {};
  }
  if(!users[user.UserName][user.Geo][user.Type]) {
    users[user.UserName][user.Geo][user.Type] = {};
  }
  if(!users[user.UserName][user.Geo][user.Type][user.month.substring(0, 3)]) {
    const { UserName, Geo,Type, month, ...weeks } = user;
    users[user.UserName][user.Geo][user.Type][user.month.substring(0, 3)] = weeks;
  }
}

console.dir(users, {depth: 10});
0 голосов
/ 06 августа 2020

const response = [{
    UserName: "User1",
    Type: "type3",
    Geo: "Geo1",
    month: "May",
    Week1: 2,
    Week2: 1
  },
  {
    UserName: "User2",
    Type: "type2",
    Geo: "Geo2",
    month: "July",
    Week1: 1
  },
  {
    UserName: "User1",
    Type: "type1",
    Geo: "Geo1",
    month: "Jun",
    Week2: 1,
    Week1: 1
  },
  {
    UserName: "User1",
    Type: "type2",
    Geo: "Geo1",
    month: "July",
    Week3: 1
  },
  {
    UserName: "User2",
    Type: "type3",
    Geo: "Geo2",
    month: "May",
    Week1: 2,
    Week2: 1
  },
  {
    UserName: "User2",
    Type: "type1",
    Geo: "Geo2",
    month: "Jun",
    Week2: 1,
    Week1: 1
  },
  {
    UserName: "User2",
    Type: "type3",
    Geo: "Geo3",
    month: "May",
    Week1: 2,
    Week2: 1
  },
];
const users = response.map(e => {
  e.UserName = e.UserName + ' ' + e.Geo;
  return e;
});
console.log(users);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...