Javascript 如何在 vueJs 中使用 forEach?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42535987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:14:30  来源:igfitidea点击:

How to use forEach in vueJs?

javascriptarraysforeachvuejs2vue.js

提问by Jaccs

I have a response like below from an API call,

我从 API 调用中得到如下响应,

response

回复

Now, I have to repeat the whole arrays inside the arrays. How do I do that in VueJS?

现在,我必须在数组内重复整个数组。我如何在 VueJS 中做到这一点?

I have searched for using forEach.. nowhere I found forEach usage like key|value pair.

我已经搜索过使用 forEach .. 没有找到像键|值对这样的 forEach 用法。

Can anyone help me on how to fetch the values from that arrays by using either forEach or any else(VueJS)?

任何人都可以帮助我了解如何使用 forEach 或任何其他(VueJS)从该数组中获取值?

Thanks & Regards,

感谢和问候,

采纳答案by rahul

In VueJSyou can use forEachlike below.

VueJS 中,您可以像下面那样使用forEach

let list=[];
$.each(response.data.message, function(key, value) {
     list.push(key);
   });

So, now you can have all arrays into list. use forloop to get values or keys

所以,现在您可以将所有数组放入list。使用for循环获取值或键

回答by Arun D Nambissan

You can also use .map() as:

您还可以将 .map() 用作:

var list=[];

response.data.message.map(function(value, key) {
     list.push(value);
   });

回答by Carolyne Stopa

This is an example of forEach usage:

这是 forEach 用法的示例:

let arr = [];

this.myArray.forEach((value, index) => {
    arr.push(value);
    console.log(value);
    console.log(index);
});

In this case, "myArray" is an array on my data.

在这种情况下,“myArray”是我数据上的一个数组。

You can also loop through an array using filter, but this one should be used if you want to get a new listwith filtered elements of your array.

您还可以使用filter循环遍历数组,但如果您想获取包含数组过滤元素的新列表,则应使用此方法。

Something like this:

像这样的东西:

const newArray = this.myArray.filter((value, index) => {
    console.log(value);
    console.log(index);
    if (value > 5) return true;
});

and the same can be written as:

同样可以写成:

const newArray = this.myArray.filter((value, index) => value > 5);

Both filter and forEach are javascript methods and will work just fine with VueJs. Also, it might be interesting taking a look at this:

filter 和 forEach 都是 javascript 方法,可以很好地与 VueJs 一起使用。此外,看看这个可能会很有趣:

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

回答by mate.gwozdz

You can use native javascript function

您可以使用原生 javascript 函数

var obj = {a:1,b:2};

Object.keys(obj).forEach(function(key){
    console.log(key, obj[el])
})

or create an object prototype foreach, but it usually causes issues with other frameworks

或者为each创建一个对象原型,但它通常会导致其他框架出现问题

if (!Object.prototype.forEach) {
  Object.defineProperty(Object.prototype, 'forEach', {
    value: function (callback, thisArg) {
      if (this == null) {
        throw new TypeError('Not an object');
      }
      thisArg = thisArg || window;
      for (var key in this) {
        if (this.hasOwnProperty(key)) {
          callback.call(thisArg, this[key], key, this);
        }
      }
    }
  });
}

var obj = {a:1,b:2};

obj.forEach(function(key, value){
    console.log(key, value)
})


回答by Mat J

The keyword you need is flatten an array. Modern javascript array comes with a flatmethod. You can use third party libraries like underscorejsin case you need to support older browsers.

您需要的关键字是展平数组。现代 javascript 数组带有一个平面方法。如果您需要支持旧浏览器,您可以使用underscorejs等第三方库。

Native Ex:

本地示例:

var response=[1,2,3,4[12,15],[20,21]];
var data=response.flat(); //data=[1,2,3,4,12,15,20,21]

Underscore Ex:

下划线例如:

var response=[1,2,3,4[12,15],[20,21]];
var data=_.flatten(response);